I have a half-dozen classes in an application annotated with @Configuration
and am writing unit tests.
I have one @Configuration
class that sets up Quartz, and one that deals with setting up Camel.
In my Camel unit test, I only want to load the @Configuration
class that sets up Camel, because it does not care about Quartz (and vice-versa).
How do I tell my Spring Boot test to only bootstrap certain configuration-annotated classes? @TestConfiguration
does not do that...
@Configuration
public class QuartzConfig {
...
}
@Configuration
public class CamelConfig {
...
}
@RunWith(SpringRunner.class)
@SpringBootTest
@SOME ANNOTATION THAT SAYS ONLY LOAD CamelConfig.class???????
public class CamelOnlyTest {
....
}
If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring. autoconfigure. exclude property.
One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.
A Spring configuration class can get quite long with many bean definitions. At this point, it can be convenient to break it into multiple classes.
@SpringBootTest is a primary annotation to create unit and integration tests in Spring Boot applications. The annotation enables additional features such as custom environment properties, different web environment modes, random ports, TestRestTemplate and WebTestClient beans.
By using the classes
parameter of @SpringBootTest
, you can specify which classes to use for configuration.
From the docs:
public abstract Class[] classes
The annotated classes to use for loading an ApplicationContext.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With