How to import configuration classes in @DataJpaTest in SpringBootTest?

I have a SpringBoot application and I am a configuration package with

@Configuration @EnableJpaAuditing public class PersistenceConfig { } 

But PersistenceConfig does not fall into PersonRepositoryTest

 @RunWith( SpringRunner.class ) @DataJpaTest public class PersonRepositoryTest { // Tests ... } 

However, if I change from @DataJpaTest to @SpringBootTest , PersonRepositoryTest will pick the configuration.

My structure

 - main - java - config PersistenceConfig.java - domain Person.java - persistence PersonRepository.java Application.java // @SpringBootApplication - test - java - persistence PersonRepositoryTest.java 

Testing for Improvements in Spring Boot 1.4 Testing the Persistence Layer with @DataJpaTest

Note: Running both annotations in the Test class still does not import the @SpringBootTest @DataJpaTest configuration

Question 1: When testing the save level using @DataJpaTest as correct (the best way in Spring Boot) to import the configuration package into my tests?

Question 2: Could work using @SpringBootTest be acceptable? I know that @DataJpaTest is also a meta annotation with reasonable auto-tuning for my database, including transaction management. But what if I don't need it?

+7
spring spring-boot spring-data-jpa unit-testing configuration
source share
3 answers

You can try the following: annotate PersistenceConfig with @ComponentScan to enable component scanning in Spring.

 @Configuration @EnableJpaAuditing @ComponentScan(basePackages = "com.yourbasepackage") public class PersistenceConfig { } 

Without additional configuration, @ComponentScan by default scans the same package as the PersistenceConfig class.

And add the @Context-Configuration annotation to tell her to load her configuration from PersistenceConfig.class.

 @RunWith( SpringRunner.class ) @DataJpaTest @ContextConfiguration(classes=PersistenceConfig.class) public class PersonRepositoryTest { // Tests ... } 
+1
source share

The solution is to use @Import to import the configuration into the configuration done with @DataJpaTest . This is my understanding of @Import .

 @RunWith(SpringRunner.class) @DataJpaTest @Import(AuditConfiguration.class) public class AuditTest { } 

with AuditConfiguration , which allows you to audit

 @Configuration @EnableJpaAuditing public class AuditConfiguration { } 
+3
source share

After @georges van post, I found that ALL configuration classes are also obtained by adding one line in the test:

 @RunWith( SpringRunner.class ) @DataJpaTest @ComponentScan(basePackages = "com.basepackage.config") public class PersonRepositoryTest { // Tests ... } 

If someone needs only ONE specific configuration class , you can do:

 @RunWith( SpringRunner.class ) @DataJpaTest @ContextConfiguration(classes=MyConfig.class) public class PersonRepositoryTest { // Tests ... } 

Or several classes with:

@ContextConfiguration(classes={MyConfig1.class, MyConfig2.class})

0
source share

All Articles