IntelliJ IDEA 2020.1 Help

Add testing libraries

The libraries for JUnit and TestNG are shipped with IntelliJ IDEA, but are not included in the classpath of your project or module by default. Consequently, when a test class is created, the references to the TestCase class or test annotations are not resolved.

To add the necessary library to the classpath, you can use the general procedure of adding a dependency to a module. The corresponding libraries are located in the following directories:

  • JUnit libraries (hamcrest-core-1.3.jar and junit-4.12.jar): <IntelliJ IDEA directory>\lib.

  • TestNG library testng-6.8.21.jar: <IntelliJ IDEA directory>\plugins\testng\lib.

IntelliJ IDEA can add the necessary library to the classpath automatically. The corresponding features are available when creating a test for a class or when writing the code for a test.

Add a test library when creating a test for a class

  1. In the editor, place the caret within the line containing the class declaration.

  2. Press Alt+Enter to open the list of available context actions and select Create Test.

    Creating a new test using context actions

  3. In the Create Test dialog, to the right of the text informing you that the corresponding library is not found, click Fix.

    Adding a test library to the classpath when creating a test for a class

  4. After the library is added, click OK.

Add a test library when writing the code for a test

  1. In the source code of a test class, place the caret within an unresolved reference to TestCase or annotation.

  2. Press Alt+Enter to open the list of available context actions and select Add <library> to classpath.

    Adding a test library when writing the code for a test

Manually add a library to a project

  1. From the main menu, select File | Project Structure Ctrl+Shift+Alt+S or click the Project Structure button on the toolbar.

  2. Select Project Settings | Libraries and click the New Project Library button | From Maven.

  3. In the dialog that opens, specify the necessary library artifact, for example: org.junit.jupiter:junit-jupiter:5.4.2.

    Manually adding a testing library to a project
  4. Apply the changes and close the dialog.

Last modified: 29 June 2020