2016-03-02 108 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); 
    }