JUnit Jupiter, a module of JUnit 5 which is the next generation of JUnit 4, comes with many of assertion methods that JUnit 4 has and adds some overloads which can be used with Java 8 Lambda Expression. In this post, we will get through some examples that use both JUnit 5 assertions borrowed from JUnit 4 and JUnit 5 assertions with Java 8 Lambda Expression.
1. Preparations.
All the source code represented in this tutorial is available on the Github project.
To run the source code, we will need IDE such as Eclipse, IntelliJ or build tools like Maven, Gradle. You can refer to my recent post for guides on how to get started with JUnit 5: JUnit 5 Basic Introduction
2. JUnit 5 Assertions Example
Assume that we will write JUnit tests for below method which is used to convert a given string into Double.
1 2 3 4 5 6 7 8 9 |
public final class StringUtils { public static Double convertToDouble(String str) { if (str == null) { return null; } return Double.valueOf(str); } } |
2.1. AssertAll, AssertNotNull, and AssertEquals
We will test the method convertToDouble with a test case that we will pass a parameter with a right Double value string. AssertAll is a new assertion in JUnit 5 which allows us to execute a group of assertions in a bigger one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Test public void testConvertToDoubleOK() { // Test case with the age is a numeric string String age = "1990"; Double expAge = Double.valueOf(age); Double actual = StringUtils.convertToDouble(age); assertAll("Do many assertions.", () -> { assertNotNull(actual); assertEquals(expAge, actual); }); // Or Java 8 Lambdas style assertAll("Do many assertions.Java 8 Lambdas style", () -> { assertNotNull(actual, () -> "The actual is NULL"); assertEquals(expAge, actual, () -> "The expected is: " + expAge + " while the actual is:" + actual); }); } |
2.2. AssertNull
In this example, we will test the method convertToDouble by passing a parameter as a NULL value.
1 2 3 4 5 6 7 8 9 |
@Test public void testConvertToDoubleWithNullArgument() { // Test case with the age is null String age = null; Double actual = StringUtils.convertToDouble(age); assertNull(actual, "The actual is not null"); // Java 8 Style assertNull(actual, () -> "The actual is not null"); } |
2.3. JUnit 5 AssertThrows
In this example, we will test the method convertToDouble by passing a not numeric string as a parameter of the method. Note that in this case, the method will call: Double.value(parameter) to convert the string to Double and we will get the NumberFormatException exception.
1 2 3 4 5 6 7 8 9 10 11 12 |
@Test public void testConvertToDoubleThrowException() { // Test with the age is a non numeric string String age = "N/A"; assertThrows(NumberFormatException.class, () -> { StringUtils.convertToDouble(age); }); assertThrows(NumberFormatException.class, () -> { StringUtils.convertToDouble(age); }); } |
Note that AssertThrows is the new assertion in JUnit 5.
2.3. AssertTrue and AssertFalse
Assume that we will write JUnit tests for the following method:
1 2 3 4 5 6 7 |
public final class StringUtils { public static boolean isNullOrBlank(String st) { return st == null || st.trim().length() == 0; } } |
And we will write 3 test cases for this method by passing: NULL, empty and not empty string as parameters of the method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Test public void testIsNullOrBlankOK() { // Test the case that the input is NULL String input = null; assertTrue(StringUtils.isNullOrBlank(input)); // Java 8 Lambdas Style assertTrue(StringUtils.isNullOrBlank(input), () -> "The string is not null or blank"); // Test case with the input is empty input = " "; assertTrue(StringUtils.isNullOrBlank(input)); // Test case with the input is not empty input = "abc"; assertFalse(StringUtils.isNullOrBlank(input)); } |
2.4. AssertSame, AssertNotSame, and Fail
Assume that we will write JUnit tests for the following method which will test if a given string is null or not. If it is null then return the given default string.
1 2 3 4 5 6 7 |
public final class StringUtils { public static String getDefaultIfNull(final String st, final String defaultSt) { return st == null ? defaultSt : st; } } |
Here is our test method which using JUnit 5 assertions: assertSame, assertNotSame, and fail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public void testGetDefaultIfNull() { // Test case with input string is null String st = null; String defaultSt = "abc"; String actual = StringUtils.getDefaultIfNull(st, defaultSt); assertSame(defaultSt, actual); // Java 8 Lambdas Style assertSame(defaultSt, actual, () -> "Expected ouput is not same with actual"); // Test case with input string is not null st = "def"; actual = StringUtils.getDefaultIfNull(st, defaultSt); assertNotSame(defaultSt, actual); // Java 8 Lambdas Style assertNotSame(defaultSt, actual, () -> "Expected ouput is same with actual"); // Test case with input string is empty st = ""; actual = StringUtils.getDefaultIfNull(st, defaultSt); if (actual.equals(defaultSt)) { fail("The actual should be empty"); // Java 8 Lambdas Style fail(() -> "The actual should be empty"); } } |
3. Test results
Just to show the tests run on Eclipse:
4. Conclusions
We have just tried to write some very basic Unit tests using JUnit 5, especially use JUnit 5 Assertions to support us in asserting the test results. We may see that JUnit 5 provides us with very basic assertions like JUnit 4. Beside, JUnit 5 provides some more assertions such as assertThrow, assertAll, and some overloads to support Java 8 Lambda Expression as well. Hope you can enjoy learning some basic steps to write Unit tests using JUnit 5 assertions. In the next posts, I’d like to share more about JUnit 5 features like assumptions, extension, etc. If you’re interested in JUnit 5, you can refer to my JUnit 5 tutorial page for more tutorials, examples.
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Disable or Ignore A Test
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Test Suite – Aggregating Tests In Suites
Display Names and Technical Names in JUnit 5