KU-Taverse / game-server-BE-

0 stars 0 forks source link

๐Ÿ’ฃ webflux test #3

Open david-parkk opened 5 months ago

david-parkk commented 5 months ago

Webflux ํ™˜๊ฒฝ์—์„œ Test

@ExtendWith(~.class)

@WebfluxTest

@Import

reference

https://velog.io/@geunwoobaek/Spring-Junit5-Test%EC%A0%95%EB%A6%AC ExtendWith https://mangkyu.tistory.com/244 WebMvcTest

david-parkk commented 4 months ago

ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

Configuration

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public TestClass(){ ....

Field

@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 Example

@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);
}

Transactional

์˜ˆ์™ธ ์ฒ˜๋ฆฌ

๊ตฌํ˜„ํ•˜๋ฉด์„œ ๋Š๋‚€๊ฒƒ

david-parkk commented 4 months ago

๋‹จ์œ„ ํ…Œ์ŠคํŠธ

GET

@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 WebTestClient webTestClient;

@Autowired UserRepository userRepository;```

InitDb

    @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));
    }
david-parkk commented 4 months ago

WebSocket Test

WebSocket

@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));
}

์ƒ๊ฐํ•ด๋ณผ ๋ฌธ์ œ

david-parkk commented 3 months ago

POST