2013-04-30 4 views
0

통합 테스트를 위해 & 통합 테스트를하고자하는 Spring MVC 3.2 프로젝트가 있습니다. 문제는 내가 가지고있는 모든 의존성 때문이며 스프린트 테스트에서도 테스트를 매우 어렵게 만든다.통합 테스트 스프링 mvc에 가장 좋은 방법

@Controller 
@RequestMapping("/") 
public class HomeController { 

    @Autowired 
    MenuService menuService; // will return JSON 

    @Autowired 
    OfficeService officeService; 


    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    @ResponseBody 
    public AuthenticatedUser rootCall(HttpServletRequest request) { 
     AuthenticatedUser authentic = new AuthenticatedUser(); 

     Office office = officeService.findByURL(request.getServerName()); 
     authentic.setOffice(office); 

     // set the user role to authorized so they can navigate the site 
     menuService.updateVisitorWithMenu(authentic); 
     return returnValue; 
    } 

이 JSON 개체를 반환합니다

는이 같은 컨트롤러를 가지고있다. 이 호출이 JSON을 사용하여 200과 올바른 객체를 반환하는지 테스트하고 싶습니다. 그러나, 나는 그 @Autowired 클래스에 의해라는 다른 클래스를 많이 가지고, 심지어 나는 경우는이처럼 그들을 조롱 :

@Bean public MenuRepository menuRepository() { 
     return Mockito.mock(MenuRepository.class); 
} 

이 조롱 클래스를 많이 만듭니다. 여기에 어떻게 그것을 테스트하기 위해 노력하고있다 :

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = JpaTestConfig.class) 
@WebAppConfiguration 
public class HomeControllerTest { 

    private EmbeddedDatabase database; 

    @Resource 
    private WebApplicationContext webApplicationContext; 

    @Autowired 
    OfficeService officeService; 

    private MockMvc mockMvc; 

    @Test 
    public void testRoot() throws Exception { mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()) 
     .andExpect(content().contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)) 
      .andExpect(content().string(<I would like canned data here>)); 

} 

은 내가 H2의 embeddeddatabase 통해 및 설정 가서 그것을 채울 수 있지만, 나는 정말이 컨트롤러 또는 응용 프로그램의 테스트입니다 궁금 수 있습니까? 누구나이 통합 테스트에 대한 더 나은 접근법을 추천 할 수 있습니까? 컨트롤러에 대한 단위 테스트는 어떻게 작성합니까?

감사합니다.

답변

1

spring show case 프로젝트를 확인하고 표준 테스트 방법을 이해하고 볼 수있는 컨트롤러 테스트 케이스를 살펴보십시오. MappingControllerTests.java에는 json 기반 컨트롤러 테스트가 있습니다.

관련 문제