Reputation: 6842
I'm reading this JUnit tutorial, where this example is reported:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class MyTests {
@Test
public void multiplicationOfZeroIntegersShouldReturnZero() {
MyClass tester = new MyClass(); // MyClass is tested
// assert statements
assertEquals("10 x 0 must be 0", 0, tester.multiply(10, 0));
assertEquals("0 x 10 must be 0", 0, tester.multiply(0, 10));
assertEquals("0 x 0 must be 0", 0, tester.multiply(0, 0));
}
}
Now, my question is: in case the test fails, how do I print the (wrong) returned result? Something like:
assertEquals("0 x 0 must be 0, instead got"+tester.multiply(0, 0), 0, tester.multiply(0, 0));
Upvotes: 1
Views: 2237
Reputation: 131496
First thing :
The example pulled from the tutorial doesn't rely on the released version of JUnit 5.
It probably relies on a JUnit 5 beta version.
public static void assertEquals(int expected, int actual, String message)
The user debugging message in case of test failure is the String
passed as last argument.
In your example, this message is passed as first argument :
assertEquals("10 x 0 must be 0", 0, tester.multiply(10, 0));
For the record, this signature comes from JUnit 4 org.junit.Assert
class where
assertEquals()
is defined as :
static public void assertEquals(String message, long expected, long actual)
I suppose that in early beta versions of JUnit 5, developers relied on the JUnit 4 signatures. But at a time, they decided to diverge with the existing (which is acceptable for a new major version).
Now JUnit 5 was released. So you should adapt your code to this stable version :
assertEquals(0, tester.multiply(10, 0), "10 x 0 must be 0");
To answer to your question, you don't need to bother about how display this message.
If the test fails, the JUnit runner will output this message for you (by default in the console and the test reports).
For example suppose I wrote a incorrect implementation of the tested method :
public class MyClass {
public int multiply(int i, int j) {
return 0;
}
}
As I execute this test class :
@Test
public void multiplicationOfZeroIntegersShouldReturnZero() {
MyClass tester = new MyClass(); // MyClass is tested
// assert statements
assertEquals(0, tester.multiply(10, 0), "10 x 0 must be 0");
assertEquals(0, tester.multiply(0, 10), "0 x 10 must be 0");
assertEquals(0, tester.multiply(0, 0), "0 x 0 must be 0");
assertEquals(10, tester.multiply(10, 1), "10 x 1 must be 10");
}
The last one assertion fails as 10 * 1
should be equal to 10
but with my flawed implementation, it returns 0
.
Now as I run this test with Eclipse, Gradle or Maven, the unit test runner displays the failure (emphasis is mine) :
Results :
Failed tests:
MyTests.multiplicationOfZeroIntegersShouldReturnZero:18
10 x 1 must be 10 ==> expected: <10> but was: <0>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
I see exactly all required information to understand the failed assertion.
User debugging message : 10 x 1 must be 10
Expected value : 10
Actual value : 0
Upvotes: 5