I am writing some mockMvc unit tests for my actuator. I currently have one for health, which works fine
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")));
}
}
This works fine. However, when apply the same logic to "/actuator/info" (literally the exact same as the health class, with only that path changed (with config defined in the application.yml, and I have reviewed this manually, it's there when I 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 format.
Edit: So the config is in the main/application.yml. When I replicate the config to the test/application.yml, it works. Is there a way to get mvc to point to my main application.yml? As all this really tests is my duplicated test config
Edit 2: Better formatting of comment:
management:
endpoints:
web:
exposure:
include:
- info
info:
application:
name: My application name
CodePudding user response:
/actuator/info provides your customized information. The default is empty information. Therefore, you must create a Spring bean to provide this information, for example:
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);
}
}
And test:
@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")));
}
}
CodePudding user response:
Problem solved. So it turns out the test resources file can't be called application.yml, it needs it's own profile, or it totally overrides the main one.
