我想弄清楚如何在使用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