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!");
}
}
최근 댓글