buildscript {
ext {
springBootVersion = '2.1.7.RELEASE' // build.gradle에 전역변수로 springBootVersion을 추가함
}
repositories {
mavenCentral() // 플러그인을 받을 레포지토리 지장
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") // 이 스크립트의 경우 플러그인으로 2.1.7.RELEASE를 사용한다는 의미임
}
}
// 자바와 스프링 부트 관련한 필수 플러그인들
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management' // 스프링 의존성 관리 플러그인
// 라이브러리(종속성)을 어디서 받을것인가?
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' // spring boot web 라이브러리
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
버전을 명시하지 않는 이유는 그래야지 "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"버전을 따라감
최종 build.gradle
buildscript {
ext {
springBootVersion = '2.1.7.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.startup'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
test {
useJUnitPlatform()
}
File > Settings > annotation 검색
Enable annotation processing 을 체크하고 설정 저장
package com.startup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.startup.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/") // http 주소가 "/" 로 들어오면
public String test(){
return "Hello world"; // 해당 String 데이터를 Response로 넘겨줌
}
}
package com.startup.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class) // Spring boot와 Junit 사이의 연결자. Junit 실행시 SpringRunner라는 스프링 실행자 사용
@WebMvcTest(controllers = TestController.class) // Web (Spring MVC)에만 집중할 수 있는 어노테이션, Controller, ControllerAdvice 사용 가능.
public class TestControllerTest {
@Autowired // Java Bean 주입
private MockMvc mvc; // 웹 API 테스트시 사용, HTTP, GET, POST 등에 대해 테스트 가능
@Test
public void test1() throws Exception {
MvcResult result = mvc.perform(get("/")) // get 요청을 / 에 보내라
.andExpect(status().isOk()) // response status를 검증 ok 가 아닐시 테스트 실패
.andExpect(content().string("Hello world")) // response body 검증, return 되는 값이 Hello world 이면 테스트 성공
.andReturn();
System.out.println(result.getResponse().getContentAsString()); // response body를 꺼내와 string으로 보여줌
}
}
출력 [테스트는 성공한다.]
... Hello world ...