Controller에서 위 머스테치에 URL 매핑을 진행 : IndexController 클래스 생성
// IndexController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
/* 머스테치 스타터 덕분에 앞의 경로와 뒤의 파일 확장자는 자동으로 지정
index를 반환하므로 (return "index";)
src/main/resources/template/index.mustache로 전환되어 View Resolver가 처리 */
@GetMapping("/")
public String index() {
return "index";
}
}
테스트 코드로 검증 : IndexControllerTest 클래스 생성
// IndexControllerTest.java
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;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class IndexControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void 메인페이지_로딩() {
// when
String body = this.restTemplate.getForObject("/", String.class);
// then
assertThat(body).contains("스프링 부트로 시작하는 웹 서비스");
}
}
실제로 URL 호출 시 페이지의 내용이 제대로 호출되는지 테스트
"/"로 호출했을 때 "스프링 부트로 시작하는 웹 서비스" 문자열이 포함되어 있는지 비교
실제로 화면이 잘 나오는지 확인 : Application.java의 main 메소드를 실행한 후 http://localhost:8080 접속