HtmlUnit 이란?

html을 단일 테스트하기 위한 tool이다.

webClient를 만들어서 사용한다. webClient로 특정 페이지에 요청을 보내고 결과를 받아서 HtmlPage라는 인터페이스를 통해 xml, text 등 여러가지로 가져올 수 있다.

form이 있는 경우 form을 채워 submit하는 테스트도 진행해 볼 수 있다.

예제

이전 thymeleaf에서 했던 테스트 예제를 HtmlUnit을 사용하여 테스트 해보겠다.

먼저 HtmlUnit을 사용하기 위한 의존성을 추가해준다.

<dependency>
	<groupId>org.seleniumhq.selenium</groupId>
	<artifactId>htmlunit-driver</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>net.sourceforge.htmlunit</groupId>
	<artifactId>htmlunit</artifactId>
	<scope>test</scope>
</dependency>

MockMvc 대신 WebClient를 주입받아 테스트 할 수 있다.

0개의 댓글