Mockito mock setter() and override getter()



我不是在Android应用程序上进行单元测试。

文本选择适配器.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){
      ...
    }
}

文本选择适配器测试.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 setter() getter() rowView
我想要从不同的输入中获得不同的答案。

我在模仿 Mockito:如何存根吸气器设置器

感谢您的关注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);
    }

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

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

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

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

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

最新更新