Spring Integration Testing Service Download Using Eureka Services

I am trying to figure out how to create integration tests in a Spring boot application that uses Eureka. Say I have a test

@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))); } } 

and I have my code for this api call:

 DiscoveryManager.getInstance().getDiscoveryClient().getApplications(); 

It will be NPE. The discoveryClient property is returned as null. The code works fine if I launch the Spring boot application directly and use the API itself. I have no specific use of the profile anywhere. Is there anything special about Eureka that I need to configure in order for the discovery client to be created for testing?

+5
source share
1 answer

Thanks to @Donovan who responded in the comments. The org.springframework.boot.test package that I did not know about has annotations created by Phillip Web and Dave Syer. A response with a modified code is required. Change class annotations to:

 @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {Application.class}) @IntegrationTest 

or if you use spring boot 1.2.1 and more

 @WebIntegrationTest @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {Application.class}) 
+6
source

All Articles