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.
--IntelliJ has been installed --The JDK has been installed
--I use Eclipse and STS for Spring Boot development, but I want to switch to IntelliJ. --I'm exhausted with JUnit4
File > New > Project
Check Gradle and Java
GroupId and ArtifactId are optional
I think this area is your choice.
No need to rewrite
wait
It should look like this.
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 ...
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.
Right-click on src / main / java in the Project tree and select New> package to create a package.
Enter the package name on the screen that opens.
The package is ready. Right-click on the resulting package again and open New> Java Class.
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.
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. .. ..
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.
Once you have a Controller, right-click on the Application class and run it.
When the Controller displays started, it is up.
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.
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
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"
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.
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.
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.
Right-click on test / java and run Run Test in ... The test runs in all cases.
As expected, the fail () method broke it.
That's it.
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