asdfsdaaf

BoardController 구현

2020. 3. 7. 23:59

 

1.

src/main/java의 org.randi.controller 패키지에서 BoardController.java를 추가해 준다.

package org.randi.controller;

import org.randi.domain.BoardVO;
import org.randi.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
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 lombok.Setter;
import lombok.extern.log4j.Log4j;

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

	@Setter(onMethod_ = @Autowired)
	private BoardService service;

	@GetMapping("/list")
	public void list (Model model) {
		log.info("list");
		
		model.addAttribute("list", service.getList());
	}
	
	@PostMapping("/register")
	public String register(BoardVO board, RedirectAttributes attrs) {
		
		log.info("register: " + board);
		
		service.register(board);
		
		attrs.addFlashAttribute("result", board.getBno());
		
		return "redirect:/board/list";
	}
	
	@GetMapping("/get")
	public void testGet(@RequestParam("bno") Long bno, Model model) { 
		log.info("/get");
		model.addAttribute("board", service.get(bno));
	}
	
	@PostMapping("/modify")
	public String modify(BoardVO board, RedirectAttributes attrs) {
		
		log.info("modify:" + board);
		if(service.modify(board)) {
			attrs.addFlashAttribute("result", "success");
		}
		
		return "redirect:/board/list";
	}
	
	@PostMapping("/remove")
	public String remove(@RequestParam("bno") Long bno, RedirectAttributes attrs) {
		log.info("remove..." + bno);
		
		if(service.remove(bno)) {
			attrs.addFlashAttribute("result", "success");
		}
		
		return "redirect:/board/list";
	}
}

 

2.

src/test/java org.randi.controller.BoardControllerTest.java를 추가해 준다

package org.randi.controller;

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.Setter;
import lombok.extern.log4j.Log4j;

@RunWith(SpringJUnit4ClassRunner.class)

@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 {

	@Setter(onMethod_ = @Autowired)
	private WebApplicationContext ctx;
	
	private MockMvc mockMvc;
	
	@Before
	public void setup() {
		this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
	}
	
	@Test
	public void testList() throws Exception { 
		log.info(
				mockMvc.perform(MockMvcRequestBuilders.get("/board/list"))
				.andReturn()
				.getModelAndView()
				.getModelMap()
			);
	}
	
	@Test
	public void testRegister() throws Exception {
		String resultPageString = mockMvc.perform(MockMvcRequestBuilders.post("/board/register")
					.param("title", "MockTest 새 글")
					.param("content", "MockTest 새 내용")
					.param("writer", "mockUser")
				)
				.andReturn()
				.getModelAndView()
				.getViewName();
		
		log.info(resultPageString);
	}
	
	@Test
	public void testGet() throws Exception {
		log.info(
				mockMvc.perform(
						MockMvcRequestBuilders.get("/board/get").param("bno", "2")
						)
						.andReturn()
						.getModelAndView()
						.getModelMap()
				);
	}
	
	@Test
	public void testModify() throws Exception {
		String resultPage = mockMvc.perform(MockMvcRequestBuilders.post("/board/modify")
				.param("bno", "3")
				.param("title", "수정된 새새 그으으을 제목")
				.param("content", "수정된 새 그으으을 내용ㅇ")
				.param("writer", "usdffew")
				).andReturn().getModelAndView().getViewName();
		
		log.info(resultPage);
	}
	
	
	@Test
	public void testRemove() throws Exception {
		//
		String resultPage = mockMvc.perform(MockMvcRequestBuilders.post("/board/remove")
				.param("bno", "456")
				).andReturn().getModelAndView().getViewName() ;
				
		log.info(resultPage);
	}
	
}

 

3.

테스트 한다.

 

 

'기타' 카테고리의 다른 글

윈도우 10 Smart Screen 끄기  (0) 2020.03.09
윈도우 10 활동 기록 지우기  (0) 2020.03.09
Git과 GitHub GitLab BitBucket 같은 것들의 차이  (0) 2020.03.05
단축키  (1) 2020.02.24
[보안] 인증서란 무엇인가?  (0) 2020.02.23

공유하기

facebook twitter kakaoTalk kakaostory naver band