What we are trying to do? Writing a Junit for Springboot actuator/Admin as below
Code snippet:
ActuatorTests.java
@SpringBootTest(properties = {
"management.endpoints.web.exposure.include=" })
@ActiveProfiles(profiles = "local")
@AutoConfigureMockMvc
public class ActuatorTests {
@Autowired
private MockMvc mockMvc;
@MockBean
JwtDecoder jwtDecoder;
@Test
public void testActuatorEndpointSuccess() throws Exception {
MockHttpServletResponse resp = mockMvc
.perform(MockMvcRequestBuilders.get("/actuator/").accept(MediaType.APPLICATION_JSON)).andReturn()
.getResponse();
assertEquals(resp.getStatus(), 200);
}
application-local.yml
This property contains Datasource, username, password and others properties
What is the issue? During spring boot container start, it is creating Data Source by using data source properties of application-local.yml Problem here is I can't rely on application-local.yml becoz properties changes environment to environment may not work all the time with same property values and also which is unnecessary for my Junit as the testcase is about testing the management actuator endpoint only.
What we have tried? Ran by excluding some the JPA classes using below.
@SpringBootTest(properties = {
"management.endpoints.web.exposure.include=" })
@ActiveProfiles(profiles = "local")
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
public class ActuatorTests { .....}
But found the below error in the console.
Note: the error log also having chain of bean creation errors from DAO,Service, to Controller layer classes, I have given only the tail of the log due to restrictions.
**Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available**
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
... 118 common frames omitted
Any help on this? We can see similar question has been asked but no answer found in it. Run junit without data source persistence context in spring boot
Any other solution to above actuator Test Junit is also welcome..