我想弄清楚如何在使用Eureka的Spring Boot应用程序上构建集成测试。说我有一个测试集成测试使用尤里卡服务的Spring Boot服务
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
public class MyIntegrationTest {
@Autowired
protected WebApplicationContext webAppContext;
protected MockMvc mockMvc;
@Autowired
RestTemplate restTemplate;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void testServicesEdgeCases() throws Exception {
// test no registered services
this.mockMvc.perform(get("/api/v1/services").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").value(jsonArrayWithSize(0)));
}
}
,我有在该API调用我的代码路径:
DiscoveryManager.getInstance().getDiscoveryClient().getApplications();
这将NPE。 discoveryClient返回为空。如果我直接启动Spring启动应用程序并自己使用API,代码就可以正常工作。我没有任何具体的配置文件用法。是否有什么特别的,我需要配置发现客户端以构建测试?
有没有什么不能/不想来运行'测试的理由@ IntegrationTest'像[这里](https://github.com/spring-cloud-samples/eureka/blob/master/src/test/java/eurekademo/ApplicationTests.java)? –
是的..你说得对。我可以使用'@ IntegrationTest'或'@ WebIntegrationTest'。无法跟上所有这些新的注释!完美解决问题。我会回答并修改其他人的代码。 – RubesMN