Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

괴발개발 개발하다

[ Spring ] 스프링 서비스 계층 설정/구현/테스트 본문

spring

[ Spring ] 스프링 서비스 계층 설정/구현/테스트

괴발새발개발자 2022. 7. 11. 01:59

 

프로젝트 구성

 

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());
	}
}