简体   繁体   English

Mockito模拟setter()和覆盖getter()

[英]Mockito mock setter() and override getter()

I am not doing a unitTesting on Android Application. 我没有在Android应用程序上执行unitTesting。

TextChoiceAdapter.java: 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: 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);
}
}

Error: 错误:
java.lang.AssertionError: Expected :null Actual :-1593868288

My question: 我的问题:
How to mock my rowView with setter() and getter() properly? 如何使用setter()getter()正确mock我的rowView
I want different answer from different input. 我想要来自不同输入的不同答案。

I am imitating Mockito: how to stub getter setter 我在模仿Mockito:如何存根getter setter

Thank you for your attentions Ferrybig and Boris van Katwijk. 感谢您的关注,Ferrybig和Boris van Katwijk。 I will follow your advice from now on. 从现在开始,我将遵循您的建议。
1. Create MockRowView class 1.创建MockRowView
2. Mock that class. 2.模拟该课。
3. For setter method use. 3.用于setter方法。 doCallRealMethod()
4. Use direct access to variable. 4.使用直接访问变量。 Since second time it called will return 0. 自第二次调用以来,它将返回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);
    }

A small but important note is that the arguments in your assertEquals are swapped. 需要注意的一点是,您的assertEquals中的参数已交换。 The first argument should be what you expect, the second what you actually got. 第一个参数应该是您期望的,第二个参数应该是您实际得到的。

The error message therefore actually indicated that you have successfully mocked your rowView object. 因此,错误消息实际上表明您已经成功rowView对象。 When you invoked getBackgroundColor() , you got null , which is what a mocked object does. 调用getBackgroundColor() ,将得到null ,这是模拟对象的作用。

You can specify behaviour for a method on a mocked object using Mockito's when mechanism: 您可以使用Mockito的when机制为模拟对象上的方法指定行为:

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

However, I feel like you are actually depending on the natural functionality of the rowView object. 但是,我觉得您实际上是在依赖rowView对象的自然功能。 You might want to not mock this object, but rather use a naturally created instance. 您可能不希望模拟该对象,而是使用自然创建的实例。 You could mock dependencies of this object if needed. 如果需要,您可以模拟该对象的依赖关系。 It does not make much sense to call a method of a mocked object in a test assertion. 在测试断言中调用模拟对象的方法没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM