[JAVA] Step notes until JUnit 5 works on Mac + IntelliJ IDEA

Introduction

It is a miscellaneous memo as the title says. Since it is a personal memorandum, it is a cut-out article without screen capture. Also, since the content is "I could do it this way", there may be a better way.

environment

Contents

JDK installation

I haven't worked on it this time because it was originally included, but I remember doing it with the following procedure. First, update Homebrew.

$ brew update

Next, install it with the following command (the latest version of Java 13 is installed at this time).

$ brew cask install java

Install IntelliJ IDEA

Since it was originally included (ry) You can just download the installer from the official website and do it, so you can omit it, right? Let's say good.

Gradle installation

Install with the following command.

$ brew install gradle

However, I was angry that it was already installed, so I reinstalled it below.

$ brew upgrade gradle

Create a project in IntelliJ IDEA

Create a project from "Create New Project". For Project SDK, specify the JDK you just installed. After that, press the Next button without making any changes, give the last Project name any name you like, and press the Finish button to finish.

Creating a source hierarchy

Create a main / java directory and a test / java directory under the original src directory. Then open File-> Project Structure ...-> Modules, select the main / java directory created above (java directory under the main directory), and select Sources with Mark as: on the screen. Also, the original src removes Sources. Similarly, select Tests in the test / java directory (java directory under the test directory).

Create build.gradle

Create the following files directly under the project directory. The purpose is to resolve the dependency of JUnit5, but I specified the latest 5.6.0 at the moment.

build.gradle


apply plugin: 'java'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform {
        includeEngines 'junit-jupiter'
    }
}

dependencies {
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.0'
}

Creating Gradle Wrapper

Execute the following command from the terminal on IDEA. You have specified the latest 6.1.1 at this time.

$ gradle wrapper --gradle-version=6.1.1

Dependency resolution

Execute the following command and wait for a while. If you can do this, you should be able to run JUnit5.

$ ./gradlew idea

Operation check

Create a suitable Java class. Here, I made the following class.

Calc.java


package com.github.dhirabayashi.calc;

public class Calc {
    public int add(int x, int y) {
        return x + y;
    }
}

When you're done, move the mouse pointer inside the method and right-click-> Go To-> Test-> Create New Test ... Make sure that JUnit5 is selected in the Testing library, and press OK to create a JUnit5 test class template. This time, I made the following test class based on it.

CalcTest.java


package com.github.dhirabayashi.calc;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalcTest {
    @Test
    public void testAdd() {
        var calc = new Calc();
        assertEquals(5, calc.add(2, 3));
    }
}

Now right click to run the test and if successful it's OK. Also, change the value and make sure it fails.

reference

Using multiple versions of Java with Brew on Mac + jEnv Notes on updating the version of Gradle wrapper No tests found for given includes Error, when running Parameterized Unit test in Android Studio How to run JUnit5 from Gradle 4.6

Recommended Posts

Step notes until JUnit 5 works on Mac + IntelliJ IDEA
[Personal notes] Frequently used IntelliJ IDEA shortcuts
JUnit 4 notes
I tried using Junit on Mac VScode Maven
Until you start developing android apps on mac
Notes on how to use each JUnit Rule
Internal commands when running Kotlin on IntelliJ IDEA