[英]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
?
我想要來自不同輸入的不同答案。
感謝您的關注,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.