我试图弄清楚如何在使用 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,代码可以正常工作。我在任何地方都没有特定的个人资料使用情况。我需要为发现客户端配置一些特殊的 Eureka 来构建测试吗?