首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Eureka服务集成测试Spring服务

使用Eureka服务集成测试Spring服务
EN

Stack Overflow用户
提问于 2015-05-21 00:35:59
回答 1查看 3K关注 0票数 5

我试图弄清楚如何在使用Eureka的Spring应用程序上构建集成测试。说我有个测试

代码语言:javascript
复制
@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调用:

代码语言:javascript
复制
DiscoveryManager.getInstance().getDiscoveryClient().getApplications();

这将是NPE。discoveryClient作为null返回。如果我直接启动Spring引导应用程序并亲自使用API,代码就能正常工作。我在任何地方都没有特定的个人资料使用。有什么特别的wrt尤里卡,我需要配置为发现客户端来构建测试?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-21 15:06:59

感谢@Donovan在评论中的回答。在我不知道的org.springframework.boot.test包中,有菲利普·Web和Dave构建的注释。想用修改后的代码给出答案。将类注释更改为:

代码语言:javascript
复制
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@IntegrationTest

或者如果您使用的是spring 1.2.1及更高版本

代码语言:javascript
复制
@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30362656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档