JAVA/Spring Boot

(프로젝트 진행) mustache를 이용한 기본 페이지 테스트.

ri5 2021. 3. 17. 15:52

- mustache 플러그인 설치

cntl + shift + a 누르고 plugin 검색후 마켓플레이스 탭누르고 mustach 검색.

 

- mustache 스타터 의존성 추가

build.gradle dependency에 추가로 등록한다.

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compileOnly('org.projectlombok:lombok:1.18.10')
    annotationProcessor('org.projectlombok:lombok:1.18.10')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')//스프링 부트용 spring Data Jpa 추상화 라이브러리
    compile('com.h2database:h2') //인메모리 관계형 데이터베이스
    compile('org.springframework.boot:spring-boot-starter-mustache')//웹 템플릿 엔진(mustach)
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

 

 

-  index.mustache를 테스트.

1. resources밑에 templates폴더 생성후 그안에 index.mustache 생성.

index.mustach 파일 위치

2. index.mustache 를 작성

<!DOCTYPE HTML>
<html>
    <head>
        <title>스프링 부트 웹서비스</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
        <h1>스프링 부트로 시작하는 웹 서비스</h1>
    </body>
</html>

기본페이지 실행시

3. src/main/web/dto에 indexController 생성

package com.jojoldu.book.springboot.web.dto;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("/")  //HTTP GET 요청을 특정 핸들러 메소드에 맵핑하기위한 annotation.
    public String index() {
        return "index";
    }
}

 

4. test/java/web에 indexControllerTest 생성

package com.jojoldu.book.springboot.web;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)//테스트용 메서드
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IndexControllerTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void mainPage_Loading(){
        //when
        String body = this.restTemplate.getForObject("/", String.class); // "/" 로 매핑된 페이지 호출

        //then
        assertThat(body).contains("스프링 부트로 시작하는 웹 서비스"); //특정 문자열이 있는지 체크;
    }
}

5. test 실행.

sucecc시