Updated February 17, 2023
Definition of JUnit 5 Assert
Junit 5 assert is the utility method used to support the asserting condition in the test suit. Those methods are accessible using assert class and assertions using junit 5. To increase the readability of as test and the assertions, it is recommended to import the respective classes which were static. Junit 5 is keeping many of the assertions methods from junit4.
What is JUnit 5 Assert?
- Junit 5 asserts will add new features not available in junit 4. When adding new features, it will take advantage of the feature of java 8.
- Also, on junit 4 assert version of library assertions will be available in all the data types which were primitive. Array and object, which contains the primitive data types.
- As per the order of the parameter, assertions are changing and moving the output of the parameter message, which is from the last parameter. Therefore, junit 5 assert that it is very useful to determine the pass or fail status of the test suite.
How can we write assertions?
- Multiple new features were added to the junit five assert, which was not present in the junit four assert.
- The steps below show how we can write the assertions in junit 5. We can assert array equals assertion will verify the actual and expected arrays are equal. Suppose our provided array is not equal; it will print the message in the input.
- If we want to write assertions in the junit 5 API, we must use the junit Jupiter API of the assertions class. This is because it provides the static assertions methods, which allow us to ensure that the specified condition is true.
- If we want to specify the error message, it will show when our assertions will fail. We must pass the message to the last parameter when the assertion fails.
- If we need to compare two values, we need to pass these two values to the invoked assertion methods.
- We are writing the assertion in junit 5 by using the assertion class. We can write the assertions by using Boolean values.
- The below example shows by using Boolean values as follows. Suppose we want to verify that our Boolean value is true; then, we need to use the assertTrue method of the class of assertion. In the below example, we have used the @DisplayName method to display the output of Boolean assertions. Also, we are using @Nested and @Test annotation to create a test suite of created java applications.
Code –
@DisplayName ("Boolean assertions")
class BooleanAssertionTest {
@Nested
@DisplayName ("Boolean is true")
class BooleanTrue {
@Test
@DisplayName ("Boolean value is true")
void BooleanIsTrue () {
assertTrue (true);
}
}
- Suppose we want to verify that the given Boolean value is false; then, we need to use the assertFalse method using the assertion class. On the other hand, suppose we want to verify the null object condition; then, we need to use the assertNull method for the assertion class.
Code
@DisplayName ("Assertions for objects")
class Junit5Assertion {
@Nested
@DisplayName("Object is null")
class ObjectIsNull {
@Test
@DisplayName("Object null")
void ObjectNull () {
assertNull (null);
}
}
}
- If we need to verify that the expected value is not equal to the equal value, then we need to use the assertNotEquals method by utilizing the assertion class.
- If we need to verify that the expected value is not null to the equal value, then we need to use the assertNotNull method by utilizing the assertion class.
Junit 5 Assert Examples
- We are creating the project name as Junit5Assert.
- This step creates the project template of junit 5 asserts in spring boot. We have provided the project group name as com.example, artifact name as Junit5Assert, project name as Junit5Assert, and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – Junit5Assert
Name – Junit5Assert
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name - com.example.Junit5Assert
Project Description - Project for Junit5Assert
- In this step, we extract the downloaded project and open the same by using the spring tool suite.
- In this step, we are checking all project structures and their files are as follows. Also, we are checking whether the pom.xml file is created or not. Suppose this file is not created, then we need to create the same manually. However, this file is created in the below example, so we do not need to create it manually.
- Add the junit dependency in the junit 5 assert project. For example, we are adding junit dependency as follows.
Code –
<dependency>
<groupId> org.junit.jupiter </groupId>
<artifactId> junit-jupiter-engine </artifactId>
<version> 5.3.1 </version>
<scope> junit-gradle </scope>
</dependency>
<dependency>
<groupId> org.junit.jupiter </groupId>
<artifactId> junit-jupiter-api </artifactId>
<version> 5.3.1 </version>
<scope> junit-gradle </scope>
</dependency>
- In the below example, we are checking how we can assert the two equal float values; we are using the assertEquals method for the same.
Code –
@DisplayName ("Assertions Example")
class AssertionTest1 {
@SuppressWarnings ("deprecation")
@Test
public void assert_float () {
float sq = 4 * 4;
float rec = 4 * 2;
assertEquals(sq, rec);
}
}
- Suppose we want to assert the default values, which differ from the expected values.
- Suppose we want to verify that the value is equal to the value we have provided; we need to use the assertEquals method with the assertion class.
Code –
@DisplayName ("Assertions writing")
class AssertionTest1 {
@Nested
@DisplayName ("Two object is equal")
class ObjectISEqual {
@Nested
@DisplayName ("Object is integer")
class ObjectInteger {
private final Integer Val1 = 9;
private final Integer Val2 = 9;
@Test
@DisplayName ("Equal")
void Equal () {
assertEquals (Val1, Val2);
} }
} }
Conclusion
Junit 5 assert will add new features which were not available in junit 4. For example, junit 5 assert is the utility method used to support the asserting condition in a test suite. Those methods are accessible using assert class and assertions using junit 5.
Recommended Articles
This is a guide to JUnit 5 Assert. Here we discuss the Definition, What is JUnit 5 Assert, How we can write assertions, and examples with code implementation. You may also have a look at the following articles to learn more –