Spring Boot

HtmlUnit

상국이 2022. 2. 28. 17:15
728x90

HtmlUnit : html을 단위테스트하기 위한 tool

 > 웹 클라이언트로 요청을 보내고 결과를 받아서 내용을 HtmlPage를 통해 값을 확인할 수 있음

 > 브라우저 타입을 줄 수 있음

 > 엘리먼트 값을 가지고와서 값을 확인할 수 있음 

 

예제)

Controller, template : 전과 동일

https://developing-countries.tistory.com/64

 

TestCode

@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {

    @Autowired
    WebClient webClient;

    @Test
    public void hello() throws IOException {
        HtmlPage page = webClient.getPage("/hello");
        HtmlHeading1 heading1 = page.getFirstByXPath("//h1");

        assertThat(heading1.getTextContent()).isEqualToIgnoringCase("sangguk");

    }
}
728x90