I am wondering, by convention, when a test fails, is it appropriate to:
For instance,
fail("Accessed the element which does not exist");
or
fail("ArrayIndexOutOfBoundException was expected but something bad happened");
Which one is generally preferred/ accepted?
The junit fail method is used to verify that the actual exception will throw an error or the test is failing at the time of development. The junit fail assertion will fails while throwing error of assertion which was unconditional.
The Assert. Fail method provides you with the ability to generate a failure based on tests that are not encapsulated by the other methods.
The fail assertion fails a test throwing an AssertionError. It can be used to verify that an actual exception is thrown or when we want to make a test failing during its development. In JUnit 5 all JUnit 4 assertion methods are moved to org.
Here, if some Test Case from your jUnit project gets "failure" or "error" result, this Test Case will be re-run one more time. Totally here we set 3 chance to get success result. So, We need to create Rule Class and add "@Rule" notifications to your Test Class.
First, if you expect the API you test to throw an exception, instead of doing a try-catch
with a fail()
...
@Test
public void testMe() {
try {
api.testThis();
fail("should not reach here");
}
catch(MyException e) {}
}
... you should do this:-
@Test(expected=MyException.class)
public void testMe() {
api.testThis();
}
That said, I rarely use fail()
. If I need to perform certain validation and the condition fails, I will most likely use assertions than using fail()
... for example:-
... instead of...
@Test
public void testMe() {
boolean bool = api.testThis();
if (!bool) {
fail("should be true because bla bla bla");
}
}
... do this:-
@Test
public void testMe() {
boolean bool = api.testThis();
assertTrue("bla bla bla",bool);
}
However, if you really need to use fail()
, be verbose and explain why it fails rather than "should not reach here" or "should fail here" because that doesn't help folks who read the testcases.
Granted, these are just simple examples.... but I think I get my points across. :)
Given that this is testing code and shouldn't make it into the final build, I'd say the more verbose the better. As such, I'd go with the first one - it's much clearer what the root cause of the problem is, which makes it easier to correct.
I'd avoid the second one under all circumstances because it's not very helpful for a tester, and if it does somehow make it into the final build its even more cryptic than the first to an end user - it simply is no help for testers or users alike.
The one other option that I'd consider is rather than indicating that an exception occurred, instead give details of the actual exception - that's why stack traces were invented. That would convey more detail than either method listed.
If there were a convention about this I would ignore it. You should say whatever will best communicate to someone seeing this message the nature of the problem in such a way that it can be resolved as easily as possible. Too often adhering to a convention fails in this regard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With