A memorandum when starting new development with IntelliJ + Gradle + SpringBoot + JUnit5 (jupiter)

Introduction

This is coal mountain water, good evening.

I'm thinking of making various things using Spring Boot myself, but as a preliminary preparation, I've compiled a memorandum to bring it to the point where development is likely to start.

There are many other articles on how to install IntelliJ and how to install the JDK, so I will give it to you, but if it is a combination of IntelliJ + Gradle + SpringBoot + JUnit5 (jupiter), I will stumble in various ways, so I will make a note so that I will not stumble every time I feel like I'm leaving.

Premise

--IntelliJ has been installed --The JDK has been installed

For people like this

--I use Eclipse and STS for Spring Boot development, but I want to switch to IntelliJ. --I'm exhausted with JUnit4

environment

Work procedure

Create a new project

File > New > Project

image.png

Check Gradle and Java

image.png

GroupId and ArtifactId are optional

image.png

I think this area is your choice.

image.png

No need to rewrite

image.png

wait

image.png

It should look like this.

Until I make a Controller that returns text appropriately for the time being

build.gradle settings

I want to use Spring, so I will rewrite build.gradle.

plugins {
    id 'java'
}

group 'net.tan3sugarless.clustering-sample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Immediately after creating the project, it should look like this.

buildscript {
    ext {
        springBootVersion = '2.1.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'

group 'net.tan3sugarless.clustering-sample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

I messed with buildscript, apply plugin, dependencies. Also, I'm erasing plugins. You may leave it ...

Creating an access point (Controller)

Then, let's make the Controller class work as an access point that is actually accessed from the Web.

For the time being, I will make it because I do not even have a package at this point.

image.png

Right-click on src / main / java in the Project tree and select New> package to create a package.

image.png

Enter the package name on the screen that opens.

image.png

The package is ready. Right-click on the resulting package again and open New> Java Class.

image.png

First, create an Application class on the opened screen. Wait a minute for Controller.

package net.tan3sugarless.clusteringsample;

public class ClusteringSampleApplication {
}

Then a clean class will be created. Let's add the description as Spring Application class to this one.

By executing this Application class, the functions as a Web server application will be launched, including the Controller class that will be added in the future.

package net.tan3sugarless.clusteringsample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClusteringSampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClusteringSampleApplication.class, args);
    }
}

Please rewrite it like this. Replace "Clustering Sample Application" with your own class name.

Now, it's finally the Controller class.

image.png

In addition, create a controller package using the same procedure as before. It doesn't have to be separate, but you can put a sticky class right underneath. .. ..

image.png

So, let's create a "DemoController" class by the same procedure as the Application class.

package net.tan3sugarless.clusteringsample.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/demo")
    public String demo(){
        return "Hello SpringBoot demo!";
    }
}

The class I made looks like this. For the time being, I just need to return anything, so use RestController. It is a pass once to return json more seriously. Display a text message.

image.png

Once you have a Controller, right-click on the Application class and run it.

image.png

When the Controller displays started, it is up.

image.png

It's really cool to use the curl command or api tester, but for the time being, from the browser

http://localhost:8080/demo

If you can access here and display the character string exactly as written in the code, you are successful!

I'd like to introduce that it's really used for lombok or futu, but I'll omit it once.

Run JUnit5 (jupiter)

I want to write tests frequently, so prepare for JUnit 5 as soon as possible.

There is still a lot of information on JUnit4 when I catch the net, but I can't live without Parameterized Test, so I set JUnit5 (jupiter).

Basically, I'm reading here, but I'll try and write it myself as a memorandum.

Baeldung | Using JUnit 5 with Gradle https://www.baeldung.com/junit-5-gradle

Addition of build.gradle

First of all, I have to enable JUnit, so I will add it to build gradle.

buildscript {
    ext {
        springBootVersion = '2.1.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'

group 'net.tan3sugarless.clustering-sample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compileOnly "org.projectlombok:lombok:1.18.8"
    testCompileOnly "org.projectlombok:lombok:1.18.8"

    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")

    // for JUnit
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.3.2'
    testCompile 'org.junit.jupiter:junit-jupiter-params:5.3.2'
    testCompile 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.3.2'
}

// for JUnit
test {
    useJUnitPlatform {
        includeEngines 'junit-jupiter'
    }
}

The code added with the comment "for JUnit"

Write a suitable test code

As long as you can confirm that it ran, it would be nice if you could see only the Assertion, aside from creating the method to be tested.

image.png

Since I don't create the method to be tested, there is no point in aligning the packages, but somehow I create the same package as src / main / java in src / test / java.

image.png

Create a test class with an appropriate name for the time being.


package net.tan3sugarless.clusteringsample.controller;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

public class DemoTest {

    static Stream<Arguments> demoProvider(){
        return Stream.of(
                Arguments.of(1,1,2),
                Arguments.of(2,2,4)
        );
    }

    @ParameterizedTest
    @MethodSource("demoProvider")
    @DisplayName("Parameterized test demo")
    void demo(int i, int j, int expected){
        Assertions.assertEquals(expected,i+j);
    }

    @Test
    void fail(){
        Assertions.fail();
    }
}

It's a very meaningless test, but please forgive me because it's a demo.

Also, I'm deliberately letting it go to see if it's working properly.

image.png

Right-click on test / java and run Run Test in ... The test runs in all cases.

image.png

As expected, the fail () method broke it.

That's it.

in conclusion

Actually I wanted to change TestRunner from Gradle or make it possible to check success cases, but that's it for today.

I hope I can explain it at another time.

I also do Twitter. ( Twitter @tan3_sugarless)

Recommended Posts

A memorandum when starting new development with IntelliJ + Gradle + SpringBoot + JUnit5 (jupiter)
Show a simple Hello World with SpringBoot + IntelliJ
Error when starting JUnit with deprecated version of POI
A memorandum when creating a REST service with Spring Boot
DataNucleus starting with Gradle
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Hello World with SpringBoot / Gradle
UnitTest with SpringBoot + JUnit + Mockito
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Naming convention when creating a new controller or model with rails