如果我有一个像下面这样的spring boot类:
@SpringBootApplication
public class DemoApplication {
@Autowired
final FoService foService;
public DemoApplication(FoService foService) {
this.foService = foService;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
和以下测试:
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = FoController.class, secure = false)
public class FoControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void getFo() throws Exception {
mockMvc.perform(get("/fo/"))
.andExpect(status().isOk());
}
}
由于无法找到,测试失败FoService
。我可以添加:
@MockBean
private FoService foService;
进行测试。或者在测试源代码树中添加新的spring boot应用程序,该应用程序不会连接服务,测试将开始工作。
我的问题是,是否还有另一种选择,也许使用excludeFilters
财产?
实际上,我从主要的Spring Boot应用程序中连接了多个服务,并且我不想全部模拟它们。同样,创建一个测试spring boot应用程序感觉更像是一种解决方法。
容易,不要在您的Main应用程序类中添加任何内容。任何Spring Boot测试或Slice测试WebMvcTest
都将使用并尝试在其中加载任何Bean或元注释。例如任何@Enable..
。
您最好将bean移到Configuration
类上,并仅SpringBootApplication
使用main方法和Main方法保持Main应用程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句