簡體   English   中英

Mockito模擬setter()和覆蓋getter()

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

我沒有在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

我的問題:
如何使用setter()getter()正確mock我的rowView
我想要來自不同輸入的不同答案。

我在模仿Mockito:如何存根getter setter

感謝您的關注,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對象的自然功能。 您可能不希望模擬該對象,而是使用自然創建的實例。 如果需要,您可以模擬該對象的依賴關系。 在測試斷言中調用模擬對象的方法沒有多大意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM