SpringBoot + Junit 예제

SpringBoot + Junit 예제

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BootrestApplicationTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testSpringBootApp() throws JsonProcessingException, IOException{
        String body = restTemplate.getForObject("/", String.class);

        assertThat(new ObjectMapper().readTree(body)
        .get("message")
        .textValue()).isEqualTo("hello World!");
    }

}

SpringBoot WebFlux + Junit 예제

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebfluxApplicationTests {
    WebTestClient webTestClient;

    @Before
    public void setup(){
        webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();
    }

    @Test
    public void testWebFluxEndpoint() throws Exception{
        webTestClient.get().uri("/")
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .expectStatus().isOk()
                .expectBody(Greet.class).returnResult()
                .getResponseBody().getMessage().equals("Hello World!");
    }

}

 

You may also like...

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.