똑같은 삽질은 2번 하지 말자
Spring Boot 개념다지기 No.12(Thymeleaf, HtmlUnit) 본문
스프링 부트가 자동 설정을 지원하는 템플릿 엔진
(FreeMarker Groovy Thymeleaf Mustache)
JSP를 권장하지 않는 이유
- JAR 패키징 할 때는 동작하지 않고, WAR 패키징 해야 함.
- Servlet Container의 개입이 있어야만 페이지가 완성된다.
- 그에 반해 , Thymeleaf는 독자적으로 페이지를 완성가능
- 그래서 andDO(print()) 테스트도 찍히는거(밑의 코드를 참조)
SampleController 를 만들고 hello 맵핑하고 Model에 name을 담아서 반환하는 테스트를 ㄱㄱ
@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {
@Autowired
MockMvc mockMvc; // 가짜 서블릿 컨테이너다. 살제 서블릿 컨테이너의 기능은 없음
@Test
public void test() throws Exception {
mockMvc.perform(get("/hello")) // 요청
.andExpect(status().isOk()) // 응답 ok
.andDo(print()) // 이게 가능한건 Thymeleaf라서
.andExpect(view().name("hello")) //뷰 이름 hello
.andExpect(model().attribute("name", "heewon")); // 모델 name : heewon
}
}
Thymeleaf 사용하기
- https://www.thymeleaf.org/
- https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
- 의존성 추가: spring-boot-starter-thymeleaf
- 템플릿 파일 위치: /src/main/resources/template/
- 예제: https://github.com/thymeleaf/thymeleafexamples-stsm/blob/3.0-master/src/main/webapp/WEB-INF/templates/seedstartermng.html
thymeleaf/thymeleafexamples-stsm
Spring Thyme Seedstarter Manager - Companion application for the "Thymeleaf + Spring 3" tutorial downloadable at the Thymeleaf website: http://www.thymeleaf.org/documentation.html - thym...
github.com
언제 한번 저 Document 읽자.. 5분이라고 하지만 5분아닌 5분같은 1시간
HTML Unit?
HTML 템플릿 뷰 테스트를 보다 전문적으로 하기위해!
HtmlUnit – Getting Started with HtmlUnit
Introduction The dependencies page lists all the jars that you will need to have in your classpath. The class com.gargoylesoftware.htmlunit.WebClient is the main starting point. This simulates a web browser and will be used to execute all of the tests. Mos
htmlunit.sourceforge.net
의존성이 필요하니깐
<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>
'Spring > Spring Boot' 카테고리의 다른 글
Spring Boot 개념다지기 No.14 (CORS) (0) | 2020.05.03 |
---|---|
Spring Boot 개념다지기 No.13(ExceptionHandler ,Spring HATEOAS) (0) | 2020.05.02 |
Spring Boot 개념다지기 No.11(Web JAR, index페이지, 파비콘) (0) | 2020.05.02 |
@Valid 와 BindingResult (0) | 2020.04.30 |
Spring Boot 개념다지기 No.10(Spring Web MVC) (0) | 2020.04.29 |