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? –

相关问题