Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring Boot Test: How to exclude java configuration class from test context

I have a Java web app with spring boot

When run test I need to exclude some Java config files:

Test config (need to include when test run):

@TestConfiguration @PropertySource("classpath:otp-test.properties") public class TestOTPConfig { } 

Production config (need to exclude when test run):

 @Configuration  @PropertySource("classpath:otp.properties")  public class OTPConfig { } 

Test class (with explicit config class):

@RunWith(SpringRunner.class) @SpringBootTest(classes = TestAMCApplicationConfig.class) public class AuthUserServiceTest { .... } 

Test config:

@TestConfiguration @Import({ TestDataSourceConfig.class, TestMailConfiguration.class, TestOTPConfig.class }) @TestPropertySource("classpath:amc-test.properties") public class TestAMCApplicationConfig extends AMCApplicationConfig { } 

Also have class:

@SpringBootApplication public class AMCApplication { } 

When test is running OTPConfig used, but I need TestOTPConfig...

How can I do it?

like image 363
Sergey Avatar asked Sep 27 '16 16:09

Sergey


People also ask

How do you exclude a class from auto-configuration in spring boot?

If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.

How do you exclude a class in Junit?

Edit Configurations > Select Code Coverage tab > then adding the package or class I want to be excluded or include only in the code coverage report.

How do you exclude beans?

You need a method with '@Bean' annotation that crate and instance of the class, or annotate the class with '@Componen', '@Service' etc. annotation for annotation scanning to find it ? Does @ComponentScan(excludeFilters = @ComponentScan. Filter(type = FilterType.

What is exclude in spring boot?

If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of @EnableAutoConfiguration to disable them, as shown in the following example: import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org ...


1 Answers

Typically you would use Spring profiles to either include or exclude Spring beans, depending on which profile is active. In your situation you could define a production profile, which could be enabled by default; and a test profile. In your production config class you would specify the production profile:

@Configuration @PropertySource("classpath:otp.properties") @Profile({ "production" }) public class OTPConfig { } 

The test config class would specify the test profile:

@TestConfiguration @Import({ TestDataSourceConfig.class, TestMailConfiguration.class,    TestOTPConfig.class }) @TestPropertySource("classpath:amc-test.properties") @Profile({ "test" }) public class TestAMCApplicationConfig extends AMCApplicationConfig { } 

Then, in your test class you should be able to say which profiles are active:

@RunWith(SpringRunner.class) @SpringBootTest(classes = TestAMCApplicationConfig.class) @ActiveProfiles({ "test" }) public class AuthUserServiceTest {   .... } 

When you run your project in production you would include "production" as a default active profile, by setting an environment variable:

JAVA_OPTS="-Dspring.profiles.active=production" 

Of course your production startup script might use something else besides JAVA_OPTS to set the Java environment variables, but somehow you should set spring.profiles.active.

like image 196
David Miller Avatar answered Sep 17 '22 11:09

David Miller