src/main/java 경로에 service 패키지 생성 후 IBoardService 인터페이스 작성
public interface IBoardService {
public void register(BoardDTO bDto) throws Exception;
public BoardDTO read(Integer bno) throws Exception;
public boolean modify(BoardDTO bDto) throws Exception;
public boolean remove(Integer bno) throws Exception;
public List<BoardDTO> listAll() throws Exception;
}
root-context 파일에 component-scan 추가
<context:component-scan base-package="com.hanul.springstudent.service"></context:component-scan>
service 패키지 안에 imbl 패키지 생성 후 BoardServiceImpl 클래스 작성
@Service
public class BoardServiceImpl implements IBoardService{
@Autowired
private IBoardDAO bDao;
@Override
public void register(BoardDTO bDto) throws Exception {
bDao.create(bDto);
}
@Override
public BoardDTO read(Integer bno) throws Exception {
return bDao.read(bno);
}
@Override
public boolean modify(BoardDTO bDto) throws Exception {
return bDao.update(bDto) == 1;
}
@Override
public boolean remove(Integer bno) throws Exception {
return bDao.delete(bno) == 1;
}
@Override
public List<BoardDTO> listAll() throws Exception {
return bDao.listAll();
}
}
src/main/java 경로에 BoardController 클래스 파일 생성 후 게시물 전체 조회 메소드 작성
@Controller
@RequestMapping("/board")
@Log4j
public class BoardController {
@Autowired
private IBoardService service;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public void listAll(Model model) throws Exception {
log.info("show all list.....................");
model.addAttribute("list", service.listAll());
}
}
src/test/java 경로에 JUnit Test Case 생성 후 등록 메소드 작성
@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 BoardControllerTest {
@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());
}
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
BoardController 클래스에 게시물 등록 메소드 추가
@RequestMapping(value = "/register", method = RequestMethod.GET)
public void registerGET() throws Exception {
log.info("register get........................");
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerPOST(BoardDTO bDto, RedirectAttributes rttr) throws Exception {
log.info("register post........................");
service.register(bDto);
rttr.addFlashAttribute("result", bDto.getBno());
return "redirect:/board/list";
}
BoardControllerTest 파일로 가서 테스트케이스 작성
@Test
public void testRegister() throws Exception {
String resultPage = mockMvc.perform(MockMvcRequestBuilders.post("/board/register")
.param("title", "새글 등록 테스트 제목")
.param("content", "새글 등록 테스트 내용")
.param("writer", "user01"))
.andReturn().getModelAndView().getViewName();
log.info("resultpage : " + resultPage);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
DB에 잘 등록되었나 확인
BoardController 클래스에 게시물 상세 조회 메소드 추가
@RequestMapping(value="/read", method = RequestMethod.GET)
public void read(@RequestParam("bno") int bno, Model model) throws Exception {
log.info("show detail.....................");
model.addAttribute("board", service.read(bno));
}
BoardControllerTest 파일로 가서 테스트케이스 작성
@Test
public void testRead() throws Exception {
String resultPage = mockMvc.perform(MockMvcRequestBuilders.get("/board/read")
.param("bno", "16"))
.andReturn().getModelAndView().getViewName();
log.info("resultpage : " + resultPage);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
BoardController 클래스에 게시물 수정 메소드 추가
// 수정
@RequestMapping(value = "/modify", method = RequestMethod.GET)
public void modifyGET(@RequestParam("bno") int bno, Model model) throws Exception {
log.info("modify get...............");
model.addAttribute("board", service.read(bno));
}
@RequestMapping(value = "/modify", method = RequestMethod.POST)
public String modifyPOST(BoardDTO bDto, RedirectAttributes rttr) throws Exception {
log.info("modify post...............");
if(service.modify(bDto)) {
rttr.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
BoardControllerTest 파일로 가서 테스트케이스 작성
@Test
public void testModify() throws Exception {
String resultPage = mockMvc.perform(MockMvcRequestBuilders.post("/board/modify")
.param("bno", "16")
.param("title", "수정 테스트 제목")
.param("content", "수정 테스트 내용"))
.andReturn().getModelAndView().getViewName();
log.info("resultpage : " + resultPage);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
DB에 잘 수정되었나 확인
BoardController 클래스에 게시물 삭제 메소드 추가
// 삭제
@RequestMapping(value = "/remove", method = RequestMethod.POST)
public String remove(@RequestParam("bno") int bno, RedirectAttributes rttr) throws Exception {
log.info("remove..................");
if(service.remove(bno)) {
rttr.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
BoardControllerTest 파일로 가서 테스트케이스 작성
@Test
public void testRemove() throws Exception {
String resultPage = mockMvc.perform(MockMvcRequestBuilders.post("/board/remove")
.param("bno", "16"))
.andReturn().getModelAndView().getViewName();
log.info("resultpage : " + resultPage);
}
빈공간 우클릭하여 JUnit Test 실행
우측 JUnit탭에 초록색으로 표시되고 하단 콘솔창에 출력이 잘 된다면 테스트 성공!
'Programming > Spring' 카테고리의 다른 글
[STS3] Mapper를 활용하여 게시판 구현(4) (0) | 2024.04.17 |
---|---|
[STS3] Mapper를 활용하여 게시판 구현(3) (0) | 2024.04.16 |
[STS3] Mapper를 활용하여 게시판 구현(1) (0) | 2024.04.15 |
[STS3] Mapper를 활용하여 DB에 데이터 조회하기 (0) | 2024.04.15 |
[STS3] Log4Jdbc Log4j2 JDBC 4 설치 (0) | 2024.04.15 |