Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jUnit fail() conventions

I am wondering, by convention, when a test fails, is it appropriate to:

  • Say why it failed (business logic)
  • Say why the message is seen (exception should have been thrown and it's not)

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?

like image 297
James Raitsev Avatar asked Feb 06 '11 03:02

James Raitsev


People also ask

What does fail () method do in JUnit?

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.

What is use of assert fail method?

The Assert. Fail method provides you with the ability to generate a failure based on tests that are not encapsulated by the other methods.

What happens when JUnit assertion fails?

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.

Is it possible to re run the failed test cases in JUnit If yes how will you do?

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.


3 Answers

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. :)

like image 57
limc Avatar answered Oct 18 '22 23:10

limc


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.

like image 35
Mac Avatar answered Oct 18 '22 22:10

Mac


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.

like image 4
Glenn Avatar answered Oct 18 '22 23:10

Glenn