file > new > other > JUnit Test Case
패키지와 이름을 설정해주고, Class under test에 Test할 Controller를 연결해준다.
Next에서 해당 메소드를 선택해주고 Finish
public class BoardControllerTest {
@Test
public void test() {
fail("Not yet implemented");
}
}
이렇게 초기화면이 되며,
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
public class BoardControllerTest {
@Autowired
private DataSource ds;
@Test
public void test() {
assertNotNull(ds);
}
}
이렇게 테스트할 환경으로 코드를 작성해준다.
ContextConfiguration에서 context path는 web.xml에 classpath가 정의되어있지 않으면
file:src로 시작해 경로를 지정해주고,
설정이 되어있다면
@ContextConfiguration(locations = {"classpath:WEB-INF/spring/root-context.xml"})
이렇게 넣어주면 된다.
해당 context에 DataSource bean 객체가 없으면 Autowired에 객체 생성이 되지 않는다.
@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 {
}
또한, 위 환경처럼 여러 xml 파일을 연결하기도 한다.
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
public class BoardControllerTest {
@Autowired
private DataSource ds;
@Autowired
private BoardService service;
@Test
public void test() {
assertNotNull(ds);
}
@Test
public void listTest() {
getList list = new getList();
list.setPageNum(1);
list.setAmount(10);
BoardVO board = new BoardVO();
List<BoardVO> list = service.getList(list);
boolean modify = service.modify(board);
}
}
조건에 맞춘 list는 에러가 없고, modify에서 Null 에러가 났다.
JUnit 창에서 각 해당하는 test메소드(test(), listTest())마다 문제있는 method를 알려주며
어떤 문제인지 Failure Trace에 로그가 나온다.
system property 기본 값, 설정 법, tomcat으로 설정 (0) | 2023.05.16 |
---|---|
spring 설정(2) controller, dao, service, serviceImpl, vo, mapper, mybatis (0) | 2023.03.30 |
controller @RequestBody, @RequestParam, @ModelAttribute 차이 (0) | 2022.11.28 |
Spring Controller 에서 Jsp 값 넘기기 (0) | 2022.11.14 |
Spring 부서목록 (select 만 있음) (0) | 2022.09.13 |