Thursday, December 9, 2010

Make Junit not stop when assertion fails

Let’s say we have multiple assertion check in one test method, Junit will stop at the first assertion error.

String str = null;
assertNotNull("String str should not be null", str);
str = "";
assertEquals("String str should have the lenght of 1", str.length(), 1);

I am using Junit class to do WebDriver test. I want to all assertions are executed no matter there is error or not. Junit Error Collector Rule allows me do that.


@Rule
public ErrorCollector errorCollector= new ErrorCollector();

@Test
public doSomeTest() {
//do something...
errorCollector.checkThat("penalty", is(savedPage.getPenalty().getValue()));
errorCollector.checkThat("exclusion", is(savedPage.getExclusion().getValue()));
}

The code above will run all the check no matter there is error or not

No comments:

Post a Comment