Issue
I just begin to try on JUnit. I have created some test cases. However, the test case will stop when I found any wrong test case. I want to run through every test case even there are many wrong test case.
e.g.
assertEquals ( "this test case will be shown", Main.plus ( 1,2 ),3 );
assertEquals ( "this first wrong test case will be shown", Main.plus ( 1, 2 ), 4 );
assertEquals ( "this first wrong test case **won't be shown**", Main.plus ( 1, 2 ), 4 );
I want to let the third this case be run (show that it is wrong)
Note: The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect all the incorrect rows in a table, and report them all at once):
More information here
http://junit.org/apidocs/org/junit/rules/ErrorCollector.html
Solution
Assertions are not test cases. A failed assertion will throw an exception which if uncaught will propagate up and the rest of the test will not be executed.
Your solution is to put each of these assertions into different tests.
Also a side note, usually the first argument to the assertion is the expected value so I'd swap the inputs.
@Test
public void correctAddition(){
assertEquals(3, Main.plus(1,2));
}
@Test
public void wrongAddition(){
//test will fail
assertEquals(4, Main.plus(1,2));
}
@Test
public void wrongAddition2(){
//test will also fail
assertEquals(4, Main.plus(1,2));
}
Answered By - dkatzel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.