Unit testing is a type of software testing where individual software units and components are tested to validate whether each code unit is performing as intended.
While in most software development companies Quality Assurance Engineers write integration tests, regression tests, system tests, user acceptance tests, etc. during the software testing phase of the Software Development Life Cycle (SDLC), unit tests are written by the software during the software development stage.
In the Java ecosystem, JUnit is a popular unit-testing framework. Numerous brand-new features based on Java 8 were added to JUnit 5.
The next generation of JUnit is JUnit 5. The objective is to establish a current foundation for JVM developer-side testing. This includes supporting a wide range of testing methods and concentrating on Java versions earlier than 8.
Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
The following code defines a minimal test class with one minimal test method.
This is an example for developing a JUnit 5 test for another class.
Let's assume that we have the following class we want to test:
A test class for the class above should look like this:
@BeforeAll and @BeforeEach
Below is an example of simple code to be executed before the main test cases:
Assertions and Assumptions
In unit tests, assertions are collections of utility methods used to back up asserting conditions. In JUnit 4, JUnit 5 and the assertions class, these utility methods can be accessed. It is always recommended to import the relevant class statically to improve the test's and assertions' readability. Without using the representing class as a prefix, in this way we are able to refer directly to the assertion method itself. You can test your code under test with the help of JUnit 5's multiple assert statements. Checking for true, false, or equality is made possible by straightforward assert statements like the one below. All of them are org-specific static methods.JUnit.Jupiter.API.Assertions.*package.
Assert statement | Example |
assertEquals | assertEquals(9, calculator.multiply(3, 3),"fail message"); |
assertTrue | assertTrue('a' < 'b', () → "fail message"); |
assertFalse | assertFalse('a' > 'b', () → "fail message"); |
assertNotNull | assertNotNull(object, "fail message"); |
assertNull | assertNull(object, "fail message"); |
Assumptions are collections of utility methods used to run tests only if certain conditions are met. They are typically used when it does not make sense to continue the test method execution. One example is when test depends on something that does not exist in the current runtime environment.
We can declare an assumption with assumeTrue(), assumeFalse() and assumingThat˙´()
In this article, only a small part of JUnit 5 has been explored. JUnit 5 makes it easy to write effective and automated software tests. There are many examples and best practices that can be found on the Internet.
Unit tests have proven that they can improve software quality, but most software engineers see them as a waste of time.
In my opinion, software developers should write unit tests because they can save them time later when errors occur.