I have Springboot application works fine which connects to the Datasource. For Junit I have disabled the Autoconfigure of DataSource by excluding the DatasourceAutoConfiguration, DataSourceTransactionManagerConfiguration, HibernateJpaAutoConfiguration classes to avoid SpringBoot Autoconfiguring the DataSource.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.5.RELEASE</version>
<dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
<dependency>
Main class
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.basepackage"})
public class SampleClass{
psvm(){
SpringApplication.run(SampleClass.class,args);
}
}
JuniTestClass
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SampleClass.class)
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SampleControllerTest {
@MockBean
private SampleService service;
@Test
public void fetchUsers() {
Mockito.when(service.retrieveUsers().thenReturn(new SampleResponse());
}
}
On mvn test, the Test scripts are run but failing, because SampleRepository bean is not Autoconfigured (might be because of exclusion of the Datasource and HibernateJpa autoconfig classes) by Springboot
Error
Caused by UnsatisfiedDependencyException: Error creating bean with name ServiceDAOImpl, nested exception No qualifying bean of type com.basepackage.repository.SampleRepository' : expected atleast 1 bean which qualifies ...................
If I remove the AutoConfig Exclusion, the Junit tests works fine in local workspace which configures Datasource (though with JUnit connectivity to DB should not be established). The issue is I am setting up devops pipeline and during "Maven Test", SpringBoot autoconfigures the DataSource and the connectivity to Jenkins to DB fails.
I would like to disable the Autoconfig during Junit - Maven Test and on actual SpringBoot jar build, the autoconfiguration should be enabled.
Can someone please let me know how to achieve this with Annotations?