반응형
Directory Structure
Application.java
package com.jacklee.webservice;
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.jacklee.webservice.web;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
void hello() throws Exception {
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("hello"));
}
}
테스트 코드 작성
HelloController 코드를 롬복으로 전환 |
mvnrepository.com/artifact/org.projectlombok/lombok
Lombok 외부라이브러리를 사용하기 위해 의존성을 추가하고, 컴파일 시 새로운 플러그인 에러이슈 떄문에 아래와 같이 의존성 및 설정을 추가한다.
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
롬복 디펜던시 추가
build.gradle
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group 'com.jacklee.webservice'
version '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
jcenter()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
package com.jacklee.webservice.web.dto;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
HelloResponseDto 생성
package com.jacklee.webservice.web.dto;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class HelloResponseDtoTest {
@Test
void 롬복_기능_테스트() {
//given
String name = "test";
int amount = 1000;
//when
HelloResponseDto helloResponseDto = new HelloResponseDto(name, amount);
//then
assertThat(helloResponseDto.getName()).isEqualTo(name);
assertThat(helloResponseDto.getAmount()).isEqualTo(amount);
}
}
롬복 기능 테스트
package com.jacklee.webservice.web;
import com.jacklee.webservice.web.dto.HelloResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
@GetMapping("/hello/dto")
public HelloResponseDto helloResponseDto(@RequestParam("name") String name,
@RequestParam("amount") int amount) {
return new HelloResponseDto(name, amount);
}
}
HelloController.helloResponseDto
package com.jacklee.webservice.web;
import org.junit.jupiter.api.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 static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest
@RunWith(SpringRunner.class)
class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
void hello() throws Exception {
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("hello"));
}
@Test
public void helloResponseDto() throws Exception {
String name = "hello";
int amount = 1000;
mvc.perform(get("/hello/dto").param("name", name).param("amount", String.valueOf(amount)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.amount", is(amount)));
}
}
HelloControllerTest
repo.yona.io/doortts/blog/issue/1
github.com/Jaekeun-Lee/jacklee-springboot-webservice