본문으로 바로가기
728x90
반응형
SMALL

1. Mybatis의 쿼리테스트

일반적인 Dao 형식과는 다르게 @Mapper - XML 네임스페이스 매핑 구조로 정의되었다.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"file:src/main/resources/egovframework/spring/context-mapper.xml",
"file:src/main/resources/egovframework/spring/context-datasource.xml"})
public class WeekReportServiceTest {

	 @Autowired
	private WeekReportMapper weekReportMapper;
	
	 //안녕하세요
	@Test
	public void 서비스코드_테스트()throws Exception{
		HashMap<String,Object> map = new HashMap();
		map.put("week_date", "2021-11-06");
		map.put("department", "130");
		
		System.out.println(map); 
		List<Map>list = new ArrayList();
		
		list = weekReportMapper.selectWeekReportDeptAndDate(map);
		
		System.out.println(list.size());
		System.out.println(list);
		
	

}
}
@RunWith(SpringJUnit4ClassRunner.class)

-JUnit 프레임워크의 실행방법을 확장할 때 사용되는 어노테이션

-SpringJUnitClassRunner.class 를 지정해주면 JUnit이 테스트 진행중 ApplicationContext를 만들과 관리해준다.

-@RunWith 는 테스트별로 각각의 객체가 생성되어도 싱글톤을 보장한다.

-JUnit5 에서는 확장된 버전인 @ExtendWith(SpringExtension.class) 어노테이션을 사용한다.

@ContextConfiguration

-주로 사용자가 컨트롤러-서비스코드-매퍼를 주입시키는 bean 처리를 하고있는 xml의 위치가 지정되는 어노테이션이다.

-로그인 테스트 시에는 시큐리티.xml 같은 로그인 관련 xml도 같이 포함시켜줘야한다.

2.Controller 테스트시

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"file:src/main/resources/egovframework/spring/context-mapper.xml",
"file:src/main/resources/egovframework/spring/context-datasource.xml"})
@WebAppConfiguration
public class AdminControllerTests {

      @Autowired
      private WebApplicationContext ctx;
      public MockMvc mvc;


      @Before
      public void setup(){
      mvc = MockMvcBuilders.webAppContextSetup(ctx).build();
      }
                                        .
                                        .
                                        .
                                        .
                                        .
                                        .

컨트롤러 테스트시 실제로 이런형식의 코드를 많이 접할 수 있다. 간단히 분석해보자.

   @Autowired
   private WebApplicationContext ctx;

-통합 테스트를 위해서 로드 된 ApplicationContext가 WebApplicationContext 이어야 함을 선언하는데 사용되는 클레스 레벨 어노테이션이다.

-테스트 클래스에 @WebAppConfiguration이 있으면 WebApplicationContext로 같이 로드해야한다.

-스프링 컨텍스트에 관한 정리글 참고해보자(https://jayviii.tistory.com/9)

MockMvc

-웹 애플리케이션을 애플리케이션 서버에 배포하지 않고도 스프링 Mvc 동작을 재현할 수 있는 클래스이다.

-Mock 의 다양한 테스트 방법이 잘 설명되있다 참고하자(https://itmore.tistory.com/entry/MockMvc-%EC%83%81%EC%84%B8%EC%84%A4%EB%AA%85)

728x90
반응형
LIST