[Spring Boot #15] 스프링 웹 MVC - HtmlUnit 써보기
- 📚 Spring/Spring Boot
- 2020. 5. 4. 17:49
HtmlUnit 이란?
- HtmlUnit은 프로그래밍적으로 HTML 사이트와 상호작용할 수 있게하는 자바 오픈소스 이다.
- 즉 Html에 특화되어 있는 테스트라고 생각하면 된다.
- 테스트 프레임워크로서 생각할 수 있지만 브라우저와 프로그래밍적으로 상호작용 할 수 있게하는 확장된 개념으로 생각하는 것이 좋다.
- 스프링4 이후에 스프링에 통합되면서 MVC 테스트(특히 템플릿 뷰 테스트) 때 유용하게 쓰인다.
- http://htmlunit.sourceforge.net/ 공식 사이트를 참고하면 좋다.
- http://htmlunit.sourceforge.net/gettingStarted.html
- 본인의 취향에 맞게 MockMvc를 쓸지 둘다 쓸지 결정해서 사용하면 된다.
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>
resources/static/templates/hello.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>kyhslam</title>
</head>
<body>
<h1 th:text="${name}">Name Power</h1>
</body>
</html>
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SampleController {
@GetMapping("/hello")
public String hello(Model model){
model.addAttribute("name","kyhslam");
return "hello";
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
//SpringApplication.run(Application.class, args);
}
}
테스트 코드
package org.kyhslam;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {
@Autowired
WebClient webClient;
@Test
public void hello() throws Exception {
HtmlPage page = webClient.getPage("/hello");
HtmlHeading1 h1 = page.getFirstByXPath("//h1");
Assertions.assertThat(h1.getTextContent()).isEqualToIgnoringCase("kyhslam");
}
}
- HtmlPage 객체를 통해 Html페이지를 자바 언어를 통해 상호작용할 수 있다.
- xPath를 통해 h1 태그의 정보를 얻어서 그것을 테스트하는 코드이다.
- 웹 페이지의 타이틀이 무엇인지 테스트할 수 있는 등 여러가지 웹 클라이언트와 상호작용할 수 있는 API를 제공한다.
- 스프링4 이상에서 스프링MVC와 통합되어 @Autowired 어노테이션을 통한 의존성 추가로 쉽게 사용할 수 있다.
'📚 Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot #17] 스프링 PostgreSQL 연동 (Docker 활용) (0) | 2020.06.04 |
---|---|
[Spring Boot #16] 스프링 ExceptionHandler (0) | 2020.05.14 |
[Spring Boot #14] 스프링 웹 MVC - Thymeleaf (0) | 2020.05.04 |
[Spring Boot #13] 스프링 웹 MVC - index페이지와 파비콘 (0) | 2020.05.04 |
[Spring Boot #12] 스프링 웹 MVC - 정적 리소스 (0) | 2020.05.03 |