#16 board : main controller, test

This commit is contained in:
haerong22
2022-08-13 03:31:42 +09:00
parent d61bbda6d8
commit f955f9da20
2 changed files with 49 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.example.board.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MainController {
@GetMapping("/")
public String root() {
return "forward:/articles";
}
}

View File

@@ -0,0 +1,36 @@
package com.example.board.controller;
import com.example.board.config.SecurityConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@Import(SecurityConfig.class)
@WebMvcTest(MainController.class)
class MainControllerTest {
private final MockMvc mockMvc;
public MainControllerTest(@Autowired MockMvc mockMvc) {
this.mockMvc = mockMvc;
}
@Test
void givenNothing_whenRequestingRootPage_thenRedirectsToArticlesPage() throws Exception {
// given
// when & then
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("forward:/articles"))
.andExpect(forwardedUrl("/articles"))
.andDo(print())
;
}
}