Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit assertion methods should be phrased in the positive or the negative?

Tags:

java

junit

assert

Should I be writing
assertTrue("User logged in", user.isLoggedIn());
or
assertTrue("User is not logged in", user.isLoggedIn());

The former provides better reading inside the source files:
"I assert that the following is true: User logged in."

The error message could be read both ways:
java.lang.AssertionError: User logged in
"There is an error in asserting that the user is logged in"
"The error is that the user is logged in."

JUnit documentation doesn't provide a clear guide to which it should be, except it is
"the identifying message for the {@link AssertionError}",
And in both cases, the text identifies the test being run.

What's the common usage?

like image 743
Steve Avatar asked Aug 25 '10 13:08

Steve


People also ask

What does assertSame method used for assertion?

What does assertSame() method use for assertion? Explanation: == is used to compare the objects not the content. assertSame() method compares to check if actual and expected are the same objects.

What is the method of assertion?

An assertion method compares the actual value returned by a test to the expected value, and throws an AssertionException if the comparison test fails.

What is the purpose of the junit assertion statements?

What is Junit Assert? Assert is a method useful in determining Pass or Fail status of a test case, The assert methods are provided by the class org.


1 Answers

How about:

assertTrue("User should be logged in", user.isLoggedIn());

Works both ways.

like image 112
Matthew Wilson Avatar answered Oct 06 '22 01:10

Matthew Wilson