Mockito mock setter()和覆盖getter()

时间:2016-03-02 08:05:11

标签: java android unit-testing

我没有在Android应用程序上进行unitTesting。

TextChoiceAdapter.java:

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我的rowView setter()getter()正确? 我想从不同的输入中得到不同的答案。

我正在模仿Mockito: how to stub getter setter

2 个答案:

答案 0 :(得分:1)

感谢您对Ferrybig和Boris van Katwijk的关注。从现在开始,我会听从你的建议 1.创建MockRowView班级 嘲笑那个班级 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);
    }

答案 1 :(得分:0)

一个小但重要的注意事项是assertEquals中的参数被交换。第一个参数应该是你所期望的,第二个是你实际得到的。

因此,错误消息实际上表明您已成功模拟了rowView对象。当您调用getBackgroundColor()时,您获得了null,这就是模拟对象的作用。

您可以使用Mockito的when机制为模拟对象指定方法的行为:

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

但是,我觉得你实际上依赖于rowView对象的自然功能。您可能不想模拟此对象,而是使用自然创建的实例。如果需要,您可以模拟此对象的依赖项。在测试断言中调用模拟对象的方法没有多大意义。