[英]Integration Testing Spring Boot service using Eureka services
I'm trying to figure out how to build integration tests on a Spring Boot application that uses Eureka. 我正在试图弄清楚如何在使用Eureka的Spring Boot应用程序上构建集成测试。 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 in my code path for that api call: 我在我的代码路径中有api调用:
DiscoveryManager.getInstance().getDiscoveryClient().getApplications();
This will NPE. 这将是NPE。 The discoveryClient is returned as null.
discoveryClient返回null。 Code works fine if I start the Spring boot app directly and use the API myself.
如果我直接启动Spring启动应用程序并自己使用API,代码工作正常。 I have no specific profile usage anywhere.
我没有任何具体的配置文件使用。 Is there something special wrt Eureka that I need to configure for the discovery client to get constructed for testing?
我是否需要为Eureka设置一些特殊的东西,以便为发现客户端进行测试构建?
Thanks to @Donovan who answered in the comments. 感谢@Donovan在评论中回答。 There are annotations built by Phillip Web and Dave Syer in the
org.springframework.boot.test
package that I was unaware of. Phillip Web和Dave Syer在
org.springframework.boot.test
包中构建了一些我不知道的注释。 Wanted to provide an answer with the changed code. 想要提供改变代码的答案。 Change the class annotations to:
将类注释更改为:
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@IntegrationTest
or if you are using spring boot 1.2.1 and greater 或者如果您使用的是1.2.1及更高版本的spring boot
@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.