👻 머스테치 플러그인 설치!
plugin 설정에 들어가서 handlebars/mustache 를 설치해 줍니다.
👻 머스테치 의존성 추가!
build.gradle 파일에 implementation 'org.springframework.boot:spring-boot-starter-mustache' 의존성을 추가해 줍니다.
🕶 index 페이지를 만들어보자!.
resources 파일의 templates 안에 index.mustache 파일을 생성해줍니다.
<!DOCTYPEHTML>
<html>
<head>
    <title>스프링 부트 웹서비스</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
    <h1>스프링</h1>
</body>
</html>
🕶 index 파일을 제어 하는 Controller 를 만들어야합니다.
@Controller
public class IndexController{
    @GetMapping("/")
    public String index() {
        return "index";
    }
}
해당 핸들러는 Dispatcher servlet 동작원리를 이해하면 알 수 있습니다.
mvc 강의 정리에 dispatcher servlet 동작원리를 참고 해보자!
👻 IndexController 클래스를 테스트 해보자!
- TestRestTemplate 사용
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class IndexControllerTest {
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Test
    @DisplayName("IndexController_TEST")
    void IndexControllerTest1() {
    // given && when
    String body = restTemplate.getForObject("/", String.class);
    // then
    assertThat(body).contains("스프링");
    }
}
TestRestTemplate 를 사용하여 body 내용이 “스프링" 이라는 문자열이 있는지 검증 합니다.
- @WebMvcTest 사용
@WebMvcTest(controllers = IndexController.class)
class IndexControllerTest{
    @Autowired
    private MockMvc mockMvc;
    @Test
    @DisplayName("IndexController_TEST2")
    void IndexControllerTest2()throws Exception{
        // given && when && then
        mockMvc.perform(get("/")
            .accept(MediaType.TEXT_HTML))
            .andDo(print())
            .andExpect(status().isOk())
            .andExpect(view().name("index"));
    }
}
mockMvc 객체를 사용하여 검증합니다.
'Book > Spring boot 와 AWS로 혼자 구현하는 웹서비스' 카테고리의 다른 글
| [Book] 18) 스프링 Security와 Oauth2.0으로 로그인 기능 구현하기! -1 (0) | 2022.04.03 | 
|---|---|
| [Book] 17) 수정 및 삭제 기능을 만들어보자! (0) | 2022.04.02 | 
| [Book] 16) 전체 조회 화면 및 기능을 만들어보자! (0) | 2022.04.02 | 
| [Book] 15) 머스테치로 화면 구성을 해보자! -2 (0) | 2022.04.01 | 
| [Book] 13) JPA Auditing으로 생성시간/수정시간 자동화 하기! (0) | 2022.04.01 | 
| [Book] 12) H2 DB 웹 콘솔에서 직접 접근해보자! (0) | 2022.04.01 | 
| [Book] 11) API 만들어보기! -4 (0) | 2022.03.31 | 
| [Book] 10) API 만들어보기! -3 (0) | 2022.03.31 |