5

我想弄清楚如何在使用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​​,代碼就可以正常工作。我沒有任何具體的配置文件用法。是否有什麼特別的,我需要配置發現客戶端以構建測試?

+2

有沒有什麼不能/不想來運行'測試的理由@ IntegrationTest'像[這裏](https://github.com/spring-cloud-samples/eureka/blob/master/src/test/java/eurekademo/ApplicationTests.java)? –

+0

是的..你說得對。我可以使用'@ IntegrationTest'或'@ WebIntegrationTest'。無法跟上所有這些新的註釋!完美解決問題。我會回答並修改其他人的代碼。 – RubesMN

回答

6

感謝@Donovan回答了評論。 Phillip Web和Dave Syer在org.springframework.boot.test軟件包中建立了註釋,這些註釋我都沒有意識到。希望爲更改後的代碼提供答案。更改類註釋:

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

,或者如果您使用的春天啓動1.2.1和更高

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

這樣可以解決啓動問題,但是如何在這個僞造的eureka客戶端中提供周圍系統的URL? –

相關問題