Issue
I am developing a test class for my application. I have 3 tests and based on the test method, I want to have different setups. Here is what my code looks like:
`public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
int counter = 0;
public MainActivityTest(Class<MainActivity> name) {
super(name);
}
public MainActivityTest() {
super(MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
switch (counter) {
case 0:
//do setup 1
break;
case 1:
//do setup 2
break;
case 2:
//do setup 3
break;
default:
break;
}
counter++;
}
public void testTest1() {
//do test 1
assertEquals(1, counter);
}
public void testTest2() {
//do test 2
assertEquals(2, counter);
}
public void testTest3() {
//do test 3
assertEquals(3, counter);
}
protected void tearDown() throws Exception {
super.tearDown();
}`
As is said in documentations, setup method is called per test. The problem is that testTest2 and testTest3 fail because counter's value is 1 in both of them. I supposed it would be incremented every time setup is called.
Solution
If you make your counter variable static, the tests will pass.
Each test gets its own instance of the test class and thus the counter value is re-initialized to the starting value of zero with every test.
Answered By - Mathias G.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.