【发布时间】:2015-08-02 11:12:33
【问题描述】:
我试图弄清楚如何在使用 Eureka 的 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 返回为 null。如果我直接启动 Spring Boot 应用程序并自己使用 API,则代码可以正常工作。我在任何地方都没有特定的个人资料使用情况。我需要为发现客户端配置一些特殊的 Wrt Eureka 以进行测试吗?
【问题讨论】:
-
您是否有理由不能/不想使用
@IntegrationTest像here 一样运行测试? -
是的..你是对的。我可以使用
@IntegrationTest或@WebIntegrationTest。跟不上所有这些新注释!完美解决问题。我会为其他人回答和修改代码。
标签: java spring-boot spring-cloud service-discovery netflix-eureka