괴발개발 개발하다
[ Spring ] 스프링 서비스 계층 설정/구현/테스트 본문
1. BoardService.java (인터페이스)
package org.pky.service;
import java.util.List;
import org.pky.domain.BoardVO;
public interface BoardService {
public int register(BoardVO board);
public BoardVO get(int board_no);
public int modify(BoardVO board);
public int remove(int board_no);
public List<BoardVO> getList();
}
2. BoardServiceImpl.java (BoardService.java 구현체)
package org.pky.service;
import java.util.List;
import org.pky.domain.BoardVO;
import org.pky.mapper.BoardMapper;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
@Service
@Log4j
@RequiredArgsConstructor
//@AllArgsConstructor
public class BoardServiceImpl implements BoardService {
//@Autowired
private final BoardMapper mapper;
@Override
public int register(BoardVO board) {
mapper.insertSelectKey(board);
log.info("register....." + board.getBoard_no());
return board.getBoard_no();
}
@Override
public BoardVO get(int board_no) {
log.info("get........." + board_no);
return mapper.read(board_no);
}
@Override
public int modify(BoardVO board) {
log.info("modify....." + board);
return mapper.update(board);
}
@Override
public int remove(int board_no) {
log.info("remove....." + board_no);
return mapper.delete(board_no);
}
@Override
public List<BoardVO> getList() {
log.info("getList.........");
return mapper.getList();
}
}
3. BoardServiceTests.java (테스트 코드)
package org.pky.service;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.pky.domain.BoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class BoardServiceTests {
@Autowired
private BoardService service;
@Test
public void testExist() {
log.info(service);
assertNotNull(service);
}
@Test
public void testRegister() {
BoardVO vo = new BoardVO();
vo.setTitle("새로 작성하는 글");
vo.setContent("새로 작성하는 내용");
vo.setWriter("새로 작성하는 사람");
int board_no = service.register(vo);
log.info("생성된 게시물의 번호 = " + board_no);
}
@Test
public void testGet() {
log.info(service.get(322));
}
@Test
public void testModify() {
BoardVO vo = service.get(322);
if(vo == null) {
return;
}
vo.setTitle("제목 수정합니다!!!!");
log.info("Modify Result: " + service.modify(vo));
}
@Test
public void testRemove() {
log.info("Remove Result: " + service.remove(322));
}
@Test
public void testGetList() {
log.info(service.getList());
}
}
'spring' 카테고리의 다른 글
[ Spring ] 스프링 영속 계층 CRUD 구현 (mapper 테스트) (0) | 2022.07.11 |
---|---|
[ Spring ] 스프링 프로젝트 생성 후 pom.xml 설정 (0) | 2022.07.10 |
[ Spring ] 스프링 게시판 만들기 테이블 생성(오라클) (0) | 2022.07.10 |
[ Spring ] 스프링 Error Page (에러 페이지) 설정 (0) | 2022.07.10 |
[ Spring ] 스프링 log 레벨 설정 (0) | 2022.07.09 |