I am trying to prevent the application from attempting to connect to the DB while running the Unit tests. Following is what I have done.
@SpringBootApplication(exclude = {
CouchbaseDataAutoConfiguration.class,
CouchbaseAutoConfiguration.class,
})
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
ApplicationStartup.class, MessageApplication.class }))
public class MessageApplicationTests {
public static void main(String[] args) {
SpringApplication.run(MessageApplicationTests.class, args);
}
}
@ActiveProfiles("test")
@SpringBootTest(classes = MessageApplicationTests.class)
class TestClass {
@Autowired
Serviceclass serviceclass;
@Test
void testMethod() {
}
}
Apart from this, I have added the following in application-test.yml
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration
- org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.messaging.MessagingAutoConfiguration
Both are not helping. Can someone help me understand what is wrong here?
Also exclude your Config class (the one that extends AbstractCouchbaseConfig) But if you have any references to repositories such as via Autowire or as args to @Service constructors, the application will fail to start. The exclude of auto configuration classes did not seem to matter when I tried it.
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { ApplicationStartup.class, MessageApplication.class, Config.class}))
Probably not related to your issue, but I found that with multiple @SpringBootApplication classes (you have MessageApplication and MessageApplicationTests, right?), that Spring goes through the auto-config classes for both of them, not just the one in
SpringApplication.run(MessageApplicationTests.class, args) ) So one would need @SpringBootApplication excludes on both classes to completely exclude them (although I found that excluding didn't change anything).