I want to Unit Test this simple activity :
public class MainActivity extends Activity implements OnClickListener {
private EditText edtValue1;
private EditText edtValue2;
private TextView txtResult;
private Button btnAdd;
private Button btnMuliply;
private ICalculator calculator;
final String LOG_TAG = "MainScreen";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtValue1 = (EditText) findViewById(R.id.value1);
edtValue2 = (EditText) findViewById(R.id.value2);
txtResult = (TextView) findViewById(R.id.result);
btnAdd = (Button) findViewById(R.id.addValues);
btnMuliply = (Button) findViewById(R.id.multiplyValues);
btnAdd.setOnClickListener(this);
btnMuliply.setOnClickListener(this);
calculator = new Calculator();
}
@Override
public void onClick(View v) {
int val1 = 0;
int val2 = 0;
try {
val1 = Integer.parseInt(edtValue1.getText().toString());
val2 = Integer.parseInt(edtValue2.getText().toString());
}
catch (NumberFormatException exc) {
Log.e(LOG_TAG, exc.getMessage(), exc);
throw exc;
}
switch (v.getId()) {
case R.id.addValues:
txtResult.setText(calculator.addNumbers(val1, val2).toString());
break;
case R.id.multiplyValues:
txtResult.setText(calculator.multiplyNumbers(val1, val2).toString());
break;
}
}
}
The facade looks like this :
Here is the simple JUnit Test Case (Please focus on the testAddDecimalValues_NumberFormatException() method):
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
MainActivity mainActivity;
private TextView txtResult;
public MainActivityTest() {
super(MainActivity.class);
}
@Before
public void setUp() throws Exception {
mainActivity = getActivity();
txtResult = (TextView) mainActivity.findViewById(R.id.result);
}
private final static String NUMBER_24 = "2 4 ENTER ";
private final static String NUMBER_74 = "7 4 ENTER ";
private final static String ADD_RESULT = "98";
@After
protected void tearDown() throws Exception {
}
@Test
public void testAddValues() {
sendKeys(NUMBER_24 + NUMBER_74 + "ENTER");
// get result
String mathResult = txtResult.getText().toString();
assertTrue("Add result should be 98", mathResult.equals(ADD_RESULT));
}
private final static String NUMBER_5_DOT_5 = "5 PERIOD 5 ENTER ";
@Test(expected = NumberFormatException.class)
public void testAddDecimalValues_NumberFormatException() {
sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + "ENTER");
}
}
When I launch Unit Test testAddValues() works perfectly but testAddDecimalValues_NumberFormatException() method doesn't take into account the expected = IllegalArgumentException.class and testAddDecimalValues_NumberFormatException() test failed...
Here is the Logcat :
[2015-05-27 12:52:42 - SimpleCalcTest] Launching instrumentation android.test.InstrumentationTestRunner on 0123456789ABCDEF
[2015-05-27 12:52:42 - SimpleCalcTest] Test run failed: Instrumentation run failed due to 'Process crashed.'
[2015-05-27 12:52:42 - SimpleCalcTest] Test run finished
[2015-05-27 12:52:43 - SimpleCalcTest] Sending test information to Eclipse
[2015-05-27 12:52:44 - SimpleCalcTest] Test run failed: Instrumentation run failed due to 'java.lang.NumberFormatException'
[2015-05-27 12:52:44 - SimpleCalcTest] Test run finished
The question is : do you know how I could test an expected exception with ActivityInstrumentationTestCase2 functionnalities ?
Thank you so much.
PS : I tried differents approach from http://ift.tt/MhlwxD but I have got the same result
Aucun commentaire:
Enregistrer un commentaire