繁体   English   中英

测试 Spring 执行器信息值

[英]Testing Spring Actuator Info Values

我正在为我的执行器编写一些 mockMvc 单元测试。 我目前有一个用于健康的,效果很好

class HealthTest {
  @Autowired
  private Mockmvc mockMvc;

  private ResultActions resultActions;

  @BeforeEach() throws Exception {
    resultActions = mockMvc.perform(get("/actuator/health"));
  }

  @Test
  void shouldReturnOk() throws Exception {
    resultActions.andExpect(jsonPath("status", is("UP")));
  }
}

这工作正常。 但是,当将相同的逻辑应用于“/actuator/info”时(字面上与运行状况 class 完全相同,仅更改了该路径(在 application.yml 中定义了配置,并且我已经手动查看了这个,当我run the application) I get a 200 status back, but no JSON, even though the web page itself shows a JSON object. It's like when I run it through this, it gets a blank page back, or the page, but not in json格式。

编辑:所以配置在 main/application.yml 中。 当我将配置复制到 test/application.yml 时,它可以工作。 有没有办法让 mvc 指向我的主 application.yml? 因为所有这些真正的测试都是我重复的测试配置

编辑2:更好的评论格式:

management:
   endpoints:
    web:
     exposure:
     include:
     - info 

info:
 application:
   name: My application name

/actuator/info提供您的自定义信息。 默认为空信息。 因此,您必须创建一个 Spring bean 来提供此信息,例如:

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class BuildInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        Map<String, String> data = new HashMap<>();
        data.put("version", "2.0.0.M7");
        builder.withDetails(data);
    }
}

并测试:

@SpringBootTest
@AutoConfigureMockMvc
class Test {

    @Autowired
    private MockMvc mockMvc;

    private ResultActions resultActions;

    @BeforeEach()
    void setUp() throws Exception {
        resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/actuator/info"));
    }

    @Test
    void shouldReturnOk() throws Exception {
        resultActions.andExpect(jsonPath("version", is("2.0.0.M7")));
    }
}
 

问题解决了。 所以事实证明测试资源文件不能被称为application.yml ,它需要它自己的配置文件,或者它完全覆盖主要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM