Home JUnit 5 - Assertions
Post
Cancel

JUnit 5 - Assertions

JUnit 5 - Assertions

JUnit 5 is a powerful testing framework for Java applications that includes a wide range of assertions to validate the behavior of your code. In this blog post, we will explore some of the commonly used JUnit 5 assertions and provide examples to demonstrate their usage. assertEquals() The assertEquals() assertion is used to check that two objects are equal. It has several overloaded variants that allow you to compare different types of objects. Here is an example:

1
2
3
4
5
6
@Test
void testMethod() {
String expected = "Hello, World!";
String actual = "Hello, World!";
assertEquals(expected, actual);
}

In this example, we are using the assertEquals() assertion to check that the expected and actual strings are equal.

assertNotEquals()

The assertNotEquals() assertion is used to check that two objects are not equal. It has several overloaded variants that allow you to compare different types of objects. Here is an example:

1
2
3
4
5
6
@Test
void testMethod() {
String expected = "Hello, World!";
String actual = "Goodbye, World!";
assertNotEquals(expected, actual);
}

In this example, we are using the assertNotEquals() assertion to check that the expected and actual strings are not equal.

assertTrue() and assertFalse()

The assertTrue() and assertFalse() assertions are used to check that a boolean expression is true or false, respectively. Here are some examples:

1
2
3
4
5
6
7
8
@Test
void testMethod() {
boolean value = true;
assertTrue(value);

    value = false;
    assertFalse(value);
}

In these examples, we are using the assertTrue() and assertFalse() assertions to check that the boolean values are true and false, respectively.

assertArrayEquals()

The assertArrayEquals() assertion is used to check that two arrays are equal. Here is an example:

1
2
3
4
5
6
@Test
void testMethod() {
int[] expected = {1, 2, 3};
int[] actual = {1, 2, 3};
assertArrayEquals(expected, actual);
}

In this example, we are using the assertArrayEquals() assertion to check that the expected and actual arrays are equal.

assertThrows()

The assertThrows() assertion is used to check that a specific exception is thrown when a certain piece of code is executed. Here is an example:

1
2
3
4
5
6
7
@Test
void testMethod() {
String str = null;
assertThrows(NullPointerException.class, () -> {
str.length();
});
}

In this example, we are using the assertThrows() assertion to check that a NullPointerException is thrown when we try to call the length() method on a null string.

assertNull() and assertNotNull()

The assertNull() and assertNotNull() assertions are used to check if an object is null or not null, respectively. Here are some examples:

1
2
3
4
5
6
7
8
9
@Test
void testMethod() {
String str = null;
assertNull(str);

    str = "Hello";
    assertNotNull(str);
}

In these examples, we are using the assertNull() and assertNotNull() assertions to check if the string str is null or not null.

assertSame() and assertNotSame()

The assertSame() and assertNotSame() assertions are used to check if two objects refer to the same object or not, respectively. Here are some examples:

1
2
3
4
5
6
7
8
9
@Test
void testMethod() {
String str1 = "Hello";
String str2 = "Hello";
assertSame(str1, str2);

    String str3 = new String("Hello");
    assertNotSame(str1, str3);
}

In these examples, we are using the assertSame() and assertNotSame() assertions to check if str1 and str2 refer to the same object, and if str1 and str3 do not refer to the same object.

assertIterableEquals()

The assertIterableEquals() assertion is used to check if two iterables contain the same elements in the same order. Here is an example:

1
2
3
4
5
6
@Test
void testMethod() {
List<Integer> expected = Arrays.asList(1, 2, 3);
List<Integer> actual = Arrays.asList(1, 2, 3);
assertIterableEquals(expected, actual);
}

In this example, we are using the assertIterableEquals() assertion to check if the expected and actual lists contain the same elements in the same order.

assertTimeout()

The assertTimeout() assertion is used to check if a piece of code runs within a certain amount of time. Here is an example:

1
2
3
4
5
6
7
8
@Test
void testMethod() {
String result = assertTimeout(Duration.ofMillis(100), () -> {
Thread.sleep(50);
return "Hello";
});
assertEquals("Hello", result);
}

In this example, we are using the assertTimeout() assertion to check if the code inside the lambda expression runs within 100 milliseconds. If the code takes longer than that, the assertion will fail.

Conclusion

In this blog post, we have explored some more JUnit 5 assertion methods. These assertions can help you create even more robust and maintainable unit tests for your Java applications.

This post is licensed under CC BY 4.0 by the author.