Adding Test Classes

Add the JUnit dependency to pom.xml, then let IntelliJ generate a test class for the class you want to test.

Apprentice · Java · Unit Testing · Updated

By

Adding JUnit unit tests to your project requires a dependency on JUnit. Open your pom.xml file at the root of your project to add the following dependency.

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Once you have added the JUnit dependency you are ready to add the test class. It is easily added by opening the class for which you want to write unit tests.

Right-click anywhere in the class and click Generate...

Right-click -> Generate…

Select Test...

Generate -> Test…

Click OK to generate the unit test class.

The Create Test dialog with JUnit5 selected and the class named TelevisionTest

A new TelevisionTest class is created for you in the test folder of your project.

TelevisionTest.java is created

Cheat sheets