Create java test class

Create tests

The simplest way of creating a new test class in IntelliJ IDEA is by using a dedicated intention action that you can invoke from your source code. In this case, the IDE creates a new test class and generates test code for this class, package, or function.

Add a new test

  1. In your production code in the editor, place the caret at the class for which you want to create a test, press Alt+Enter , and select Create Test .
  2. In the Create Test dialog, select the library that you want to use. If you don’t have the necessary library yet, you will be prompted to download it. To do that, click Fix . If you’re using Maven, the IDE will add the missing dependencies to your pom.xml . For Gradle projects, add the necessary dependencies manually.
  3. Configure the test class name and its location and select the methods that you want to test. Click OK . As a result, IntelliJ IDEA creates a new test class with the specified name and generated test methods in the Test Sources Root .

Create Test dialog controls

Select the testing framework that you are going to use.

This button is available when a library for the selected testing framework is missing. Click it to download and install the necessary library.

Читайте также:  Пример веб-страницы с php кодом

If you’re using Maven, the IDE will add the missing dependencies to your pom.xml . For Gradle projects, add the necessary dependencies manually.

Enter the name for the test class or accept the default name. You can change the way test classes are named in the settings.

For JUnit3, the superclass junit.framework.TestCase is suggested automatically. For the other supported frameworks, this field is blank.

Specify the name of the package where the generated test class will be stored.

Include stub methods for test fixtures and annotations into the generated test class.

Select this option to show all methods, including the inherited ones.

Generate test methods for

Select the methods for which you want to generate test methods.

Create a new test class manually

  1. Right-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class . Creating a new test class manually
  2. Name the new class and press Enter .
  3. Press Alt+Insert and select Test Method to generate a new test method for this class. Name the new method and press Enter . Generating a new test method

Configure naming pattern for test classes

By default, IntelliJ IDEA adds the Test suffix to class names when generating test classes. For example, if you create a test class for HelloWorld , its name by default is HelloWorldTest . You can change the naming pattern in the settings.

  1. In Settings Ctrl+Alt+S , go to Editor | Code Style | Java , and open the Code Generation tab.
  2. In the Naming section of the tab, locate the Test fields and type a suffix or a prefix (or both) that you want to use for generated test classes into the corresponding fields.

In IntelliJ IDEA, you can jump between test classes and production code.

Navigate to test from production code

  • In the editor, place the caret at the test class or at the test subject in the source code and press Ctrl+Shift+T ( Navigate | Test Subject or Navigate | Test ).

If there’s only one test for this class, the IDE will navigate you to it right away. Otherwise, you will be prompted to select the necessary test from a popup or create a new test.

Источник

JUnit 5

In this tutorial, you will learn how to set up JUnit for your projects, create tests, and run them to see if your code is operating correctly. It contains just the basic steps to get you started.

If you want to know more about JUnit, refer to the official documentation. To learn more about testing features of IntelliJ IDEA, refer to other topics in this section.

You can choose to follow the tutorial using either Maven or Gradle.

Create a project

  1. From the main menu, select File | New | Project .
  2. Select New Project . Specify the name for the project, for example, junit-tutorial .
  3. Select Maven as a build tool. In Language , select Java .
  4. From the JDK list, select the JDK that you want to use in your project. If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory. If you don’t have the necessary JDK on your computer, select Download JDK .
  5. Click Create .

For more information on working with Maven projects, refer to Maven.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

The Load Maven Changes button

  1. Open pom.xml in the root directory of your project. To quickly navigate to a file, press Ctrl+Shift+N and enter its name.
  2. In pom.xml , press Alt+Insert , select Add dependency .
  3. In the tool window that opens, type org.junit.jupiter:junit-jupiter in the search field. Locate the necessary dependency in the search results and click Add next to it.
  4. Now we need to apply the changes in the build script. Press Ctrl+Shift+O or click Load Maven Changes in the notification that appears in the top-right corner of the editor.

Create a project

  1. From the main menu, select File | New | Project .
  2. Select New Project . Specify the name for the project, for example, junit-tutorial .
  3. Select Gradle as a build tool. In Language , select Java .
  4. From the JDK list, select the JDK that you want to use in your project. If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory. If you don’t have the necessary JDK on your computer, select Download JDK .
  5. Click Create .

For more information on working with Gradle projects, refer to Gradle.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

The Load Gradle Changes button

  1. Open build.gradle in the root directory of your project. To quickly navigate to a file, press Ctrl+Shift+N and enter its name.
  2. In build.gradle , press Alt+Insert , select Add dependency .
  3. In the tool window that opens, type org.junit.jupiter:junit-jupiter in the search field. Locate the necessary dependency in the search results and click Add next to it.
  4. Now we need to apply the changes in the build script. Press Ctrl+Shift+O or click Load Gradle Changes in the notification that appears in the top-right corner of the editor.

Create a project

  1. From the main menu, select File | New | Project .
  2. Select New Project . Specify the name for the project, for example, junit-tutorial .
  3. Select IntelliJ as a build tool. In Language , select Java .
  4. From the JDK list, select the JDK that you want to use in your project. If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory. If you don’t have the necessary JDK on your computer, select Download JDK .
  5. Click Create .

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

Manually adding a testing library to a project

  1. From the main menu, select File | Project Structure ( Ctrl+Alt+Shift+S ) or click on the toolbar.
  2. Under Project Settings , select Libraries and click | From Maven .
  3. In the dialog that opens, specify the necessary library artifact, for example: org.junit.jupiter:junit-jupiter:5.9.1 .
  4. Apply the changes and close the dialog.

The procedure above shows the ‘manual’ way so that you know what happens behind the scenes and where you set up the testing framework. However, if you just start writing tests, IntelliJ IDEA will automatically detect if the dependency is missing and prompt you to add it.

Write application code

Let’s add some code that we’ll be testing.

  1. In the Project tool window Alt+1 , go to src/main/java and create a Java file called Calculator.java .
  2. Paste the following code in the file:

Create tests

Now let’s create a test. A test is a piece of code whose function is to check if another piece of code is operating correctly. In order to do the check, it calls the tested method and compares the result with the predefined expected result . An expected result can be, for example, a specific return value or an exception.

  1. Place the caret at the Calculator class declaration and press Alt+Enter . Alternatively, right-click it and select Show Context Actions . From the menu, select Create Test . The code of the class that we are going to test
  2. Select the two class methods that we are going to test. The Create Test dialog
  3. The editor takes you to the newly created test class. Modify the add() test as follows:

@Test @DisplayName(«Multiply two numbers») void multiply() < assertAll(() ->assertEquals(4, Calculator.multiply(2, 2)), () -> assertEquals(-4, Calculator.multiply(2, -2)), () -> assertEquals(4, Calculator.multiply(-2, -2)), () -> assertEquals(0, Calculator.multiply(1, 0))); >

To navigate between the test and the code being tested, use the Ctrl+Shift+T shortcut.

Run tests and view their results

After we have set up the code for the testing, we can run the tests and find out if the tested methods are working correctly.

The popup that appears after clicking on the Run icon in the gutter

  • To run an individual test, click in the gutter and select Run .
  • To run all tests in a test class, click against the test class declaration and select Run .

You can view test results in the Run tool window.

The results of the tests shown in the Run tool window

IntelliJ IDEA hides passed tests by default. To see them, make sure the Show Passed option is enabled in the Run tool window.

Источник

Оцените статью