我们的系统中有许多 Controller 和许多 Spring Data 存储库。
我想为通过我的 MVC 上下文运行的 Controller 编写测试。
但是,必须手动模拟系统中的每个服务和存储库,以便我可以测试 Controller ,这似乎非常麻烦,而且也不正确
例如
FooControllerTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy(value = {
@ContextConfiguration(classes = { MockServices.class }),
@ContextConfiguration({ "classpath:/META-INF/spring/mvc-servlet-context.xml" }),
})
public class FooControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mvc;
@Autowired
private FooRepository fooRepository;
@Autowired
private FooService fooService;
@Before
public void setUp() throws Exception {
mvc = webAppContextSetup(wac).build();
}
@Test
public final void list() {
when(fooRepository.findAll()).thenReturn(...);
mvc.perform(get("/foo"))...
}
@Test
public final void create() {
Foo fixture = ...
when(fooService.create(fixture)).thenReturn(...);
mvc.perform(post("/foo"))...
}
}
MockServices.java
@Configuration
public class MockServices {
@Bean
public FooRespository fooRepositiory() {
return Mockito.mock(FooRespository.class);
}
@Bean
public FooService fooService() {
return Mockito.mock(FooService.class);
}
//even though we are "only" testing FooController, we still need to mock BarController's dependencies, because BarController is loaded by the web app context.
@Bean
public BarService barService() {
return Mockito.mock(FooService.class);
}
//many more "mocks"
}
我真的不想使用standaloneSetup()
(想使用生产配置,例如转换服务、错误处理程序等)
这就是我到目前为止编写 Controller 测试所必须付出的代价吗?
似乎应该有类似模拟每个用@Service注释的类
或模拟每个扩展JpaRepository的接口(interface)
最佳答案
MVC Controller 通常像将模型与 View 集成在一起的粘合代码一样实现。例如,当从 Controller 调用 EJB,然后更新 View 模型时。
因此,当您确实模拟所有依赖项并验证此集成或“粘合代码”是否按预期工作时, Controller 测试可能有意义。一般来说,如果集成测试意味着太多组件,则可能需要对整个系统进行模块化才能使系统真正可测试。
无论如何,如果您发现集成测试很费力,也许您可以尝试为每个独立组件获得最大的覆盖率,并让功能测试获得 Controller 的覆盖率。
关于java - Spring MVC Controller 测试,并模拟许多类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476143/