life is egg
22.12.13 [단위 테스트] 본문
TDD와 단위 테스트는 다르다 ..
TDD는 테스트가 주도하는 개발 로써..
- 항상 실패하는 테스트를 먼저 작성
- 테스트가 통과하는 프로덕션 코드를 작성
- 테스트가 통과하면 프로덕션 코드를 리팩토링
단위테스트는... TDD의 첫번째 단계인 기능 단위의 테스트 코드를 작성하는것..
책이 19년도 판인데 3년사이에 뭔가 많이 바뀌었나보다 더럽게 오류뜬다....
이책은 gradle 4.x인데 나는 벌써 7.x이다 이미 적는방법부터가 매우 다르다
그래도 좋은책이라니 하는데 화나는 이유는!
일단은 여기저기 돌아다니면서 참고하면서 해결했다 junit 4에서 5로바뀐게 뭔가 많이 달라져서 그런듯..
일단 gradle 세팅 적는방법이 달라졌다
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.6'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
요건 Test할 컨트롤러 코드
package com.example.awsspinrgboot.web.controller;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//@RunWith(SpringRunner.class) //< 이건 junit 4에서 적용이 끝나서 그레이들에 따로 설정해야하나봐~.
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello가_리턴됨() throws Exception{
String hello = "hello";
mvc.perform(get("/hello")).andExpect(status().isOk()).andExpect(content().string(hello));
}
}
(@RunWith(SpringRunner.class) < 이건 junit 4에서 쓰던거인데 5부터는 달라졌다
>@ExtendWith(~) 이렇게 바뀌었다
>> 역활은... 스프링부트 테스트아 jUnit 사이의 연결자 역할
@WebMvcTest
- Web에 집중할 수 있는 어노테이션..
- @Controller @Controller Advice 등을 사용가능.... but @Sevice @Componenet @Repository 사용불가
@Autowired
- 스프링이 관리하는 빈을 주입받는다 (아마 스프링이 생성해주는 인스턴스를 주입받는 다는 말인듯.!)
private MockMvc
- 웹 API 테스트할때 사용
- 스프링 MVC의 시작점...
- HTTP GET,POST등의 API 테스트할수 있다
mvc.perform(요청메소드 ("/url"))
- url로 요청메소드를 보냄 ... 체이닝 지원... 여러 검증 가능하다
- .andExpect(status().isOk()) >HTTP Header의 status를검증 /200 , 404 , 500등 의 상태검증 OK이면 200이다 ..!
- .andExpect(content().String(" ")) >응답내용이 맞나 확인하는거 내가 작성한 컨트롤러가 응답을 스트링으로해줘서그래
이렇게 작성하고 실행하면 통과가 되었다고 뜨면 정상적으로 응답요청 잘받는다는것..

이걸 원본 Application에서도 수동으로 실행해서 확인해보면...

이렇게 콘솔창에 뜬다
중요한건 테스트 코드로 먼저 검증하고... 정 못 믿겠다 싶을 때.... 수동으로 프로젝트실행확인...
즉 순서가 반대가 되면 안된다...
'TIL' 카테고리의 다른 글
| 22.12.19 [정리 귀찮] (0) | 2022.12.20 |
|---|---|
| 22.12.14 [Junit5를 곁들인 메모..] (0) | 2022.12.14 |
| 22.12.12 [DDD+AOP 찍먹..] (0) | 2022.12.12 |
| 22.12.09 [~.~] (0) | 2022.12.09 |
| 22.12.08 [갑분 HTML] (0) | 2022.12.09 |
Comments