我有一个Spring MVC测试:
@RunWith(SpringRunner.class)
@WebMvcTest
@Configuration
public class InventoryControllerIntegrationTest {
@MockBean
private InventoryService inventoryService;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnInventory() throws Exception {
this.mockMvc.perform(get("/inventory")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")));
}
}
和相应的控制器。
运行测试后,我得到了异常:
2018-01-27 21:28:25.099 ERROR 12470 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@6df37f7a] to prepare test instance [com.video.rental.store.videorentalstore.inventory.InventoryControllerIntegrationTest@21d3d6ec]
java.lang.IllegalStateException: The @PropertyMapping annotation @WebMvcTest cannot be used in combination with the @Component annotation @Configuration
at org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer$PropertyMappingCheckBeanPostProcessor.postProcessBeforeInitialization(PropertyMappingContextCustomizer.java:99) ~[spring-boot-test-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
如何解决此问题并添加模拟bean inventoryService?