보라코딩

스프링 Controller 테스트 (WebApplicationContext, MockMvc 사용) 본문

코딩/Spring

스프링 Controller 테스트 (WebApplicationContext, MockMvc 사용)

new 보라 2023. 5. 11. 12:28

매번 Tomcat 실행하지 않고 스프링과 웹 URL 테스트 하는 방법 ???

=>  WebApplicationContext 와 MockMvc 이용하기 !!

 

 

BoardController.java

 

package com.mystudy.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mystudy.domain.BoardVO;
import com.mystudy.service.BoardService;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;

@Controller
@Log4j
@RequestMapping("/board/*")
@AllArgsConstructor
public class BoardController {

private BoardService service;

@GetMapping("/list")
public void list(Model model) {

log.info("list");
model.addAttribute("list", service.getList());
}

@PostMapping("/register")
public String register(BoardVO boardVO, RedirectAttributes rttr) {

log.info("register : " + boardVO);

service.register(boardVO);

rttr.addFlashAttribute("result", boardVO.getBno());

return "redirect:/board/list";
}

@GetMapping("/get")
public void get(@RequestParam("bno") Long bno, Model model) {

log.info("/get");
model.addAttribute("board", service.get(bno));
}

@PostMapping("/modify")
public String modify(BoardVO boardVO, RedirectAttributes rttr) {

log.info("modify : " + boardVO);

if (service.modify(boardVO)) {
rttr.addFlashAttribute("result", "success");
}

return "redirect:/board/list";
}

@PostMapping("/remove")
public String remove(@RequestParam("bno") Long bno, RedirectAttributes rttr) {

log.info("remove..." + bno);

if(service.remove(bno)) {
rttr.addFlashAttribute("result", "success");
}

return "redirect:/board/list";
}


}

 

 

 

 

 

 

 

 

 

 

BoardControllerTests.java

 

package com.mystudy.spring;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import lombok.extern.log4j.Log4j;

@RunWith(SpringJUnit4ClassRunner.class)
// Test for Controller
@WebAppConfiguration
@ContextConfiguration(
{"file:src/main/webapp/WEB-INF/spring/root-context.xml",
 "file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
@Log4j
public class BoardControllerTests {

@Autowired
private WebApplicationContext ctx;

private MockMvc mockMvc;

@Before // 테스트 전 매번 실행하는 메서드
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
}

 


@Test   //get 방식
public void testList() throws Exception {
log.info(
mockMvc.perform(MockMvcRequestBuilders

.get("/board/list"))
.andReturn()
.getModelAndView()
.getModelMap()          // model 객체 전달할때 사용!
);
}


@Test   //post 방식
public void testRegister() throws Exception {

log.info(

mockMvc.perform(MockMvcRequestBuilders

.post("/board/register")
.param("title", "새글 제목이요")
.param("content", "새글 내용이요")
.param("writer", "새글 작성자요")
).andReturn()
.getModelAndView()
.getViewName());          // 마지막에 retun .. 한 view name을 보여준다.
}

 

@Test
public void testGet() throws Exception {
log.info(
mockMvc.perform(MockMvcRequestBuilders
.get("/board/get")
.param("bno", "3"))
.andReturn()
.getModelAndView()
.getModelMap() // model 객체 전달할때 사용!
);
}

 


@Test
public void testModify() throws Exception {
log.info(mockMvc.perform(MockMvcRequestBuilders
.post("/board/modify")
.param("bno", "1")
.param("title", "수정된 테스트 새글 제목")
.param("content", "수정된 테스트 새글 내용")
.param("writer", "수정된 테스트 작성자"))
.andReturn()
.getModelAndView()
.getViewName());
}

 


@Test
public void testRemove() throws Exception{

log.info(mockMvc.perform(MockMvcRequestBuilders
.post("/board/remove")
.param("bno", "20"))
.andReturn()
.getModelAndView()
.getViewName());
}

'코딩 > Spring' 카테고리의 다른 글

jstl 태그 (날짜 포맷)  (0) 2023.05.11
부트스트랩 sb-admin-2  (0) 2023.05.11
스프링 Service CRUD 테스트  (0) 2023.05.11
스프링 Oracle, mybatis 연동, CRUD 테스트  (0) 2023.05.10
STS와 GitHub 연동  (0) 2023.05.10