2016-03-02 110 views
0

我沒有在Android應用上做單元測試。

TextChoiceAdapter.java:Mockito模擬二傳手()和重寫getter()

public class TextChoiceAdapter extends ArrayAdapter<String> { 
    public Context context; 
    public int selectedPosition = -1; //Otherwise Android set zero then choice A will be selected automatically 
    public void choiceSelection(View rowView, int position){ 
     if (selectedPosition == position) 
      rowView.setBackgroundColor(0xA0FF8000); // orange 
     else 
      rowView.setBackgroundColor(Color.TRANSPARENT); 
    } 
    public TextChoiceAdapter(Context context,int resources, List<String> textChoiceList) { 
     super(context, resources, textChoiceList); 
     this.context = context; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     ... 
    } 
} 

TextChoiceAdapterTest.java:

public class TextChoiceAdapterTest{ 
    private TextChoiceAdapter textChoiceAdapter; 
    private ArrayList<String> textChoiceList; 
    @Before 
    public void setUp(){ 
     textChoiceList = new ArrayList<>(); 
     textChoiceList.add("North"); 
     textChoiceList.add("East"); 
     textChoiceList.add("West"); 
     textChoiceList.add("South"); 
     Context context = mock(Context.class); 
     textChoiceAdapter = new TextChoiceAdapter(context, 1, textChoiceList); 
    } 
    @Test 
     public void testChoiceSelection(){ 
    //https://stackoverflow.com/questions/10217793/mockito-how-to-stub-getter-setter 

    textChoiceAdapter.selectedPosition = 1; 
    Context context = mock(Context.class); 

    //Try my own object class. 
    class mockRowView extends View{ 
     int backgroundColor; 
     public mockRowView(Context context){ 
      super(context); 
     } 
     public void setBAckgroundColor(int a){ 
      this.backgroundColor = a; 
     } 
     public int getBackgroundColor(){ 
      return this.backgroundColor; 
     } 
    } 
    View rowView = mock(mockRowView.class); 
    textChoiceAdapter.choiceSelection(rowView, 1); 
    assertEquals(rowView.getBackgroundColor(), 0xA0FF8000); 
} 
} 

錯誤:
java.lang.AssertionError: Expected :null Actual :-1593868288

我的問題:
如何mock我的rowViewsetter()getter()正確嗎?
我想從不同的輸入不同的答案。

我模仿Mockito: how to stub getter setter

+0

'查看rowView = new mockRowView();'? – Ferrybig

+0

我不明白爲什麼這個測試需要使用getter。是'View rowView = mock(View.class); textChoiceAdapter.choiceSelection(rowView,1);驗證(rowView).setBackgroundColor(0xA0FF8000);'不是你需要的?如果不是,你能澄清爲什麼不? – JustATrick

回答

0

一個小而重要的一點是,在您assertEquals的參數進行交換。第一個參數應該是你期望的,第二個參數應該是你實際得到的。

因此錯誤消息實際上表明您已成功嘲笑您的rowView對象。當你調用getBackgroundColor()時,你得到了null,這是一個嘲弄的對象。

您可以使用的Mockito的when機制嘲笑對象的方法指定行爲:

Mockito.when(rowView.getBackgroundColor()).thenReturn(42); 

不過,我覺得你實際上是取決於rowView對象的自然功能等。你可能不想模擬這個對象,而是使用自然創建的實例。如果需要,你可以模擬這個對象的依賴關係。在測試斷言中調用模擬對象的方法沒有多大意義。

1

謝謝您的關注Ferrybig和Boris van Katwijk。從現在開始我會聽從你的建議。
1.創建MockRowView
2.模擬該類。
3.用setter方法使用。 doCallRealMethod()
4.使用直接訪問變量。由於第二次調用將返回0.

@Test 
    public void testChoiceSelection(){ 

     textChoiceAdapter.selectedPosition = 1; 
     Context context = mock(Context.class); 

     //Try my own object class. 
     class MockRowView extends View{ 
      int backgroundColor; 
      public MockRowView(Context context){ 
       super(context); 
      } 
      @Override 
      public void setBackgroundColor(int a){ 
       this.backgroundColor = a; 
      } 
      //User direct access will not cause a problem when do assertEquals() 
     } 

     MockRowView rowView = mock(MockRowView.class); 
     doCallRealMethod().when(rowView).setBackgroundColor(anyInt()); 

     textChoiceAdapter.selectedPosition = 2; 
     textChoiceAdapter.choiceSelection(rowView, 1); 
     assertEquals(rowView.backgroundColor, Color.TRANSPARENT); 
     textChoiceAdapter.choiceSelection(rowView, 2); 
     assertEquals(rowView.backgroundColor, 0xA0FF8000); 
    }