Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Test - I do not want to load all @Configuration classes for a particular test

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 {
    ....
}
like image 787
mikeb Avatar asked Mar 22 '18 01:03

mikeb


People also ask

How do I exclude a configuration class in spring boot?

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.

Do we need @configuration in spring boot?

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.

Can we have multiple @configuration in spring?

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.

What does SpringBootTest annotation do?

@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.


1 Answers

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.

like image 79
ThisIsNoZaku Avatar answered Oct 29 '22 17:10

ThisIsNoZaku