Additional Info:
The Hello World! application was generated with JUnit 4, but let’s upgrade to JUnit 5, so we have the latest features and bug fixes.
Determine new JUnit version
Go to the JUnit Jupiter’s Sonatype site and determine which version to use. At the time of this writing, the latest release was 5.9.3. I tend to avoid the ones with suffixes like -M1 or -RCE, as they look to be works in progress.
As shown on the Apache Maven snippet from that site, the way to specify that dependency in the pom.xml file is by using:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
</dependency>
Make the updates to our pom.xml
In our case, we are replacing JUnit 4 with JUnit 5, so we want to replace the following dependency info in the java101/pom.xml with the code snippet above.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<dependency>
After making the change, we need to refresh the Maven project (Ctrl + Shift + O) to allow IntelliJ to consume the changes. If an icon with “m” with a refresh circle on it appears, you can alternatively click on that to manually refresh.
You can also tell IntelliJ to auto-reload on Maven changes in the File Menu -> Settings dialog (Ctrl+Alt+S) -> Build, Execution, Deployment | Build Tools -> Select Any changes -> Click Apply -> Click OK
Update the JUnit Test’s Imports
In the java101/src/test/java/codingchica/java101/AppTest.java, change the existing imports:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
This will be changed to the following to reflect where these classes exist in the newer version of JUnit 5/Jupiter:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
Even though we changed the dependencies for the project as a whole in the pom.xml, by updating the import statements we tell the Java compiler which dependencies we now intend to use in this particular file/class.
Just like when we ran the Maven archetype and provided a package name, each of our dependencies get to choose the folder structure (or package) their own code resides within. As seen above, different versions of a dependency may move files/classes around.
Import vs. Import Static
Why does one import statement have a static keyword, while the other doesn’t?
We will dive deeper into this later, but the short explanation is that:
- static – means that it applies to a class as a whole, rather than working on an instance of that class
- import – Tells the compiler we want to use the file as a whole and will reference it as its own entity (class).
- import static – Works for methods / functions inside of the other class we are referencing that are also declared as static (class-level) – allows us to call those methods without specifying which class it came from.
In this case, by specifying
import static org.junit.jupiter.api.Assertions.assertTrue;
instead of
import org.junit.jupiter.api.Assertions;
within the rest of the file, we can say things like:
assertTrue( true );
instead of having to say:
Assertions.assertTrue( true );
Upgrade the Maven Plugin for Unit Testing
The maven-surefire-plugin automatically binds to the test / unit testing phase of the Maven build. As we’re updating which unit test framework is in use, let’s also upgrade this plugin to a newer version to make sure it works correctly with JUnit 5.
In the pom.xml, change:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.</version>
</plugin>
…to instead show the most recent version seen in the maven-surefire-plugin’s site:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
Just like before, we will need to tell IntelliJ to consume this update using (Ctrl + Shift + O) or one of the other methods mentioned above.
Run the Unit Test
Inside of java101/src/test/java/codingchica/java101/AppTest.java, along the left hand side of the display (along the left gutter), you should see 2 green arrows overlapping to the left of the line that reads:
public class AppTest
Click on those overlapping green arrows -> Run ‘AppTest’
The Run view should appear and after a moment, the Test Results should display with a green check mark to the left. See also: IntelliJ: Run Tests.
Run the Maven Build
Before finalizing any change, it is good to make sure that the project successfully builds.
In this case, we changed both the JUnit dependency and the maven-surefire-plugin versions, we want to make sure they work together to successfully run the unit tests, too. When you run mvn clean install you should look for both:
# Confirmation that the existing unit test was run successfully:
[INFO] Running codingchica.java101.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 s -- in codingchica.java101.AppTest
...
# Overall build success
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Commit
Remember, commit small changes frequently. This would be a good time.

Leave a comment