IntelliJ IDEA 2017.1 Help

Adding and Testing Java Classes in a new Gradle Project

Creating a new Gradle Project

  1. Open Project Wizard, in the left-hand pane select Gradle.
  2. In the right-hand pane, IntelliJ IDEA automatically adds a project SDK (JDK) and a default option Java in the Additional Libraries and Frameworks area. You can edit this information if you like.
    /help/img/idea/2017.1/gradle_new_project.png
    Click Next.
  3. On the next page of the wizard let's specify ArtifactId which basically the name of our project. We can use the default information in the version field. Unless we plan to deploy our project in some Maven repository we don't need to specify a GroupId.
    /help/img/idea/2017.1/gradle_groupId.png
    Click Next.
  4. On the next page of the wizard, let's leave the default options such as Create separate module per source set and Use default gradle wrapper (recommended) selected. We will specify two more options:
    • Use auto-import - to resolve all the changes made to the Gradle project automatically every time we refresh our project.
    • Create directories for empty content roots automatically - to create the default directory structure for our Gradle project, e.g. /src/main/java and /src/test/java.
    Click Next.
    /help/img/idea/2017.1/gradle_settings_page.png
  5. We've already specified our project's name, let's specify the location of our project and click Finish.
    /help/img/idea/2017.1/gradle_project_info_page.png

IntelliJ IDEA creates a project with build.gradle file and src folder with main and test subdirectories.

/help/img/idea/2017.1/gradle_project_view.png

IntelliJ IDEA also creates a dedicated tool tool window with default tasks.

/help/img/idea/2017.1/gradle_tool_window.png
For more information on creating a Gradle project with the options that are out of this scope, see Gradle.

Adding Java and test classes to a Gradle project

Let's add Java and test classes to our Gradle project.

Note that if you didn't select the Create directories for empty content roots automatically option while creating a project you can select this option on the Gradle page in the Settings dialog (Ctrl+Alt+S). So IntelliJ IDEA can add the src directory to the project.

/help/img/idea/2017.1/gradle_page_project_settings.png
  1. In the project view open the src folder.
  2. Right-click on the main or test directory then on the java subdirectory and from the list that opens select New | Java Class.
    /help/img/idea/2017.1/gradle_add_java_class.png
  3. In the Create New Class dialog specify the name of your Java or test class and click OK.
    /help/img/idea/2017.1/gradle_create_new_class_dialog.png

Let's add the following code:

  • for our HelloWorld class -
    /help/img/idea/2017.1/gradle_helloworld_example.png
  • for our Test class -
    /help/img/idea/2017.1/gradle_test_example.png

Running tests in a Gradle project

We can run our test in several different ways:

  • using the editor - click /help/img/idea/2017.1/run.png in the left gutter of the editor.
    /help/img/idea/2017.1/gradle_run_test_editor.png
  • using the Gradle task test - in the Gradle tool window open Tasks directory and then verification subdirectory. In the list that opens, double-click test to run your test.
    /help/img/idea/2017.1/gradle_run_test_task.png
  • using the Delegate all IDE builds/run actions to gradle option in the Settings dialog.
    /help/img/idea/2017.1/gradle_delegate_to_gradle_option.png

In all these cases the result of the test will be displayed in the Run tool window.

/help/img/idea/2017.1/gradle_test_result.png

See Also

Last modified: 18 July 2017