I have a SpringBoot Application and I a config package with
@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}
But the PersistenceConfig does not get picked up in a PersonRepositoryTest
@RunWith( SpringRunner.class )
@DataJpaTest
public class PersonRepositoryTest {
// Tests ...
}
However, if I change from @DataJpaTest to @SpringBootTest
, PersonRepositoryTest will pick up the config.
My package structure is
- main
- java
- config
PersistenceConfig.java
- domain
Person.java
- persistence
PersonRepository.java
Application.java // @SpringBootApplication
- test
- java
- persistence
PersonRepositoryTest.java
The Testing improvements in Spring Boot 1.4 suggest to test the persistence layer with @DataJpaTest
Observation: Doing both annotations on the Test class still do not import the config @SpringBootTest @DataJpaTest
Question 1: When testing the Persistence Layer with @DataJpaTest how do I properly (best practise way in Spring Boot) import the config package into my Tests?
Question 2: Can it be an acceptable work around using @SpringBootTest? I am aware that @DataJpaTest is also a meta annotation with sensible auto configuration for my database including transaction management. But what If I do not need it?
The @SpringBootTest annotation loads the complete Spring application context. In contrast, a test slice annotation only loads beans required to test a particular layer. And because of this, we can avoid unnecessary mocking and side effects.
@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests.
Using @TestConfiguration in Unit Tests As explained earlier, we can use the @TestConfiguration annotation in two ways during testing: Import test configuration using the Import annotation. Declaring @TestConfiguration as a static inner class.
@SpringBootTest This annotation works by creating the ApplicationContext used in our tests through SpringApplication. It starts the embedded server, creates a web environment and then enables @Test methods to do integration testing. By default, @SpringBootTest does not start a server.
A solution is to use @Import
to import your configuration to the configuration done by @DataJpaTest
. This is my understanding of @Import
.
@RunWith(SpringRunner.class)
@DataJpaTest
@Import(AuditConfiguration.class)
public class AuditTest {
}
with AuditConfiguration
that enables auditing
@Configuration
@EnableJpaAuditing
public class AuditConfiguration {
}
You can try this:
annotate PersistenceConfig
with @ComponentScan
to enable component scanning in Spring.
@Configuration
@EnableJpaAuditing
@ComponentScan(basePackages = "com.yourbasepackage")
public class PersistenceConfig {
}
With no further configuration, @ComponentScan
will default to scanning the same package as the PersistenceConfig
class.
And add the @Context-Configuration
annotation to tell it to load its configuration from the PersistenceConfig.class.
@RunWith( SpringRunner.class )
@DataJpaTest
@ContextConfiguration(classes=PersistenceConfig.class)
public class PersonRepositoryTest {
// Tests ...
}
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