Spring mvc 集成测试SpringMVC的最佳方法
我有一个SpringMVC3.2项目,我想进行单元和集成测试。问题是我所有的依赖性,使得即使使用Sprint测试,测试也非常困难 我有一个这样的控制器:Spring mvc 集成测试SpringMVC的最佳方法,spring-mvc,spring-test,integration-testing,Spring Mvc,Spring Test,Integration Testing,我有一个SpringMVC3.2项目,我想进行单元和集成测试。问题是我所有的依赖性,使得即使使用Sprint测试,测试也非常困难 我有一个这样的控制器: @Controller @RequestMapping( "/" ) public class HomeController { @Autowired MenuService menuService; // will return JSON @Autowired OfficeService officeSe
@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;
}
@Bean public MenuRepository menuRepository() {
return Mockito.mock(MenuRepository.class);
}
这将返回一个JSON对象。我想测试这个调用返回一个200和一个正确的带有固定JSON的对象。然而,我有许多其他类被@Autowired类调用,即使我这样模仿它们:
@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;
}
@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>));
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JpaTestConfig.class)
@WebAppConfiguration
公共类家庭控制器测试{
私有嵌入式数据库;
@资源
私有WebApplicationContext WebApplicationContext;
@自动连线
办公服务办公服务;
私有MockMvc-MockMvc;
@试验
public void testRoot()引发异常{mockMvc.perform(get(“/”).andDo(print()).andExpect(status().isOk())
.andExpect(content().contentType(IntegrationTestUtil.APPLICATION\u JSON\u UTF8))
.andExpect(content().string());
}
我可以设置一个H2嵌入式数据库并填充它,但我想知道这是否真的是对这个控制器或应用程序的测试?有人能推荐一些更好的集成测试方法吗?如何为控制器编写单元测试
谢谢!查看该项目并查看控制器测试案例,您将能够理解并看到测试控制器的标准方法。有一些基于json的控制器测试