我有一个要测试的方法,该方法具有签名公共无效processBalance(EditText incomeField,HashMap yearsMappedToObjectYearsMap,boolean isPaymentCircleSet,TextView balanceLabel),并且位于util类MainActivityUtil中。 我的MainActivity是
public class MainActivity extends AppCompatActivity implements IHODClientCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
....
mainUtil = new MainActivityUtil(this);
incomeField = new EditText(this);
incomeField = (EditText) findViewById(R.id.incomeField);
isPaymentCircleSet = sharedprefs.getBoolean(ISPAYMENTCIRCLE, false);
balanceLabel = new TextView(this);
balanceLabel = (TextView) findViewById(R.id.balanceView);
yearsMappedToObjectYearsMap = new HashMap<>();
yearsMappedToObjectYearsMap = util.readTheFile();
//and the method later is called
mainUtil.processBalance(incomeField, yearsMappedToObjectYearsMap,isPaymentCircleSet,balanceLabel);
}
}
我想编写一个Junit测试来测试此方法,我已经知道到目前为止我可以使用Mockito:
@RunWith(MockitoJUnitRunner.class)
public class MainActivityTests {
@Mock
private Context context;
@Mock
private EditText incomeField;
@Mock
MainActivity mMockMainActivity;
@Test
public void testProcessBalance(){
MainActivityUtil mainUtil = new MainActivityUtil(context);
incomeField = (EditText) mMockMainActivity.findViewById(R.id.incomeField);
incomeField.setText(1000);
HashMap<String, AnyYear> yearsMappedToObjectYearsMap = new HashMap<>();
boolean isPaymentCircleSet = false;
TextView balanceLabel = new TextView(context);
mainUtil.processBalance(incomeField,yearsMappedToObjectYearsMap,isPaymentCircleSet,balanceLabel);
}
我看到IncomeField为null,这是错误的。任何帮助将不胜感激。