I've got the following test which I have put a try catch around so it fails if actual value does not equal expected value:
try{
Assert.assertEquals(ExpectedCount, ActualCount);
}catch (Throwable t){
System.out.println ("Actual value is not equal to expected value");
}
Whenever I run this test it passes. However if expected=actual no message is printed, which is correct. If they are not equal then the message i printed. So the test logic is valid.
However the Juint test still passes.
I have been trying the incorporate the following but cannot get it to work:
(Added this at public level in the class of the test)
@Rule
Public ErrorCollected errCollector = new ErrorCollector();
Added:
errCollector.addError(t);
Under
System.out.println ("Actual value is not equal to expected value");
I am getting the error back "Rule cannot be resolved to a type
" It does not give me any option to import.
I've been trying to find out how use the @Rule
method to make the test to fail, but cannot figure it out.
As @oers says, the reason you can't find Rule is probably because your version of JUnit is too old. Try a later version, > 4.7.
The easiest solution to your problem is just to rethrow the exception that you're catching:
try {
Assert.assertEquals(ExpectedCount, ActualCount);
} catch (Throwable t) {
System.out.println ("Actual value is not equal to expected value");
throw t;
}
but you'll need to add throws Throwable to your test method. You have another alternative, to add a description to your assert:
Assert.assertEquals("Actual value is not equal to expected value", ExpectedCount, ActualCount);