Open david-parkk opened 7 months ago
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public TestClass(){ ....
@ExtendWith(SpringExtension.class)
์ ๊ฐ์ ์ด๋
ธํ
์ด์
์ด ์์ต๋๋ค.SpringBootTest.WebEnvironment.RANDOM_PORT
์ ๊ฐ์ด ํฌํธ ์ง์ ์ ํด์ค์ผํฉ๋๋ค.(์ง์ ์ํ๋ฉด denpendency error ๋ฐ์)@Autowired
private ApplicationContext applicationContext;
@Autowired
WebTestClient webTestClient;
@Autowired
UserRepository userRepository;```
- ApplicationContext : ์คํ๋ง ์ปจํ
์ด๋ ์ฃผ์
(์ํด๋ @SpringBootTest์ ์ํด ์๋์ผ๋ก ์ค์ ๋๋ ๊ฑฐ ๊ฐ๋ค)
- WebTestClient: API ํธ์ถ ๊ฐ์ฒด
### InitDb
```java
@PostConstruct
public void initDB(){
Mono<Void> saveUsers = userRepository.save(user1)
.then(userRepository.save(user2))
.then();
StepVerifier.create(saveUsers)
.expectSubscription()
.verifyComplete();
}
@Test
@DisplayName("get ์์ฒญ์ผ๋ก ์ ์ ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.")
public void test1(){
webTestClient = WebTestClient.bindToApplicationContext(applicationContext).configureClient().responseTimeout(Duration.ofHours(1)).build();
webTestClient.get()
.uri("/user/1")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(User.class)
.isEqualTo(user1);
}
@Test
@DisplayName("unit test-API test ์ ์ฅ๋ ์ ์ ๋ฅผ ์ฐพ์์ผํ๋ค.")
public void test2() {
//given
String userId = "1";
Mockito.when(userService.findOne("1")).thenReturn(Mono.just(user));
//when
webTestClient.get()
.uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.userId").isEqualTo(userId);
//then
Mockito.verify(userService, Mockito.times(1)).findOne(userId);
}
@Autowired
private ApplicationContext applicationContext;
@Autowired WebTestClient webTestClient;
@Autowired UserRepository userRepository;```
@Test
@DisplayName("unit test-API test user๋ฅผ ์ ์ฅํ์ ๋ user์ ๋ํ return ๊ฐ์ ๋ฐ์ ์์ผ ํ๋ค. " +
"์์
์ด ํ์ํฉ๋๋ค")
public void test1() {
//given
Mockito.when(userService.create(postMapUserRequest)).thenReturn(Mono.just(postMapUserResponse));
//when
webTestClient.post()
.uri("/user")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(postMapUserRequest), PostMapUserRequest.class)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(UserService.class)
Mockito.verify(userService, Mockito.times(1)).create(refEq(postMapUserRequest));
}
Mockito.verify(userService, Mockito.times(1)).create(refEq(postMapUserRequest));
์์ refEq์ ์ฌ์ฉํ์๋๋ฐ API๋ฅผ ํธ์ถํ๋ ์ญ์ง๋ ฌํ๊ณผ์ ์์ ๊ฐ์ฒด๊ฐ ๋ฌ๋ผ์ ธ์ ๊ฐ์ฒด ์ฃผ์๊ฐ ๋ฌ๋ผ์ง๋๋ค@DisplayName("[integration test] ์ ์ฅ๋ map ์ ์ ์ ๋ณด๋ฅผ websocket๋ก ๋ฐ์์ผ ํ๋ค.")
@Test
public void test1() throws URISyntaxException {
int connectionTimeSecond=1;
AtomicInteger counter = new AtomicInteger(0);
WebSocketClient client = new ReactorNettyWebSocketClient();
Exception exception = assertThrows(IllegalStateException.class, () -> client.execute(getUrl("/map"), session -> session.receive()
.doOnNext(data -> {
List<UserResponseDto> userResponseDtos = JsonUtil.jsonToUserList(data.getPayloadAsText());
assertTrue(verifyResponse(userResponseDtos,userResponseDtoList));
counter.incrementAndGet();
})
.then()).block(Duration.ofSeconds(connectionTimeSecond)
)
);
String expectedMessage = "Timeout on blocking read for 1";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
IllegalStateException.class
Webflux ํ๊ฒฝ์์ Test
@ExtendWith(~.class)
@ExtendWith (SpringExtension.class)
,@ExtendWith (MockitoExtension.class)
์ผ๋ก ์ค์ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ ๊ฐ Extension.class์์ Spring context์ junit5์ ํตํฉํด์ ์ฌ์ฉํ๋๋ก ์ ๊ณตํ๊ณ ์๋ค (Mockito๊ฐ ๋ ๊ฐ๋ณ์ต๋๋ค)@RunWith(SpringRunner.class)
๋ฅผ ์ฌ์ฉ@SpringBootTest
,@WebfluxTest
์ด๋ ธํ ์ด์ ์ ์ด์ด๋ณด๋ฉด@ExtendWith (SpringExtension.class)
๋ฅผ ํ์ธ ํ ์ ์์์ต๋๋ค.@WebfluxTest
@Import
reference
https://velog.io/@geunwoobaek/Spring-Junit5-Test%EC%A0%95%EB%A6%AC ExtendWith https://mangkyu.tistory.com/244 WebMvcTest