How do I import configuration classes in a @DataJpaTest in a SpringBootTest?

21,150

Solution 1

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 {
}

Solution 2

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 ...
}
Share:
21,150

Related videos on Youtube

Dachstein
Author by

Dachstein

Updated on November 16, 2020

Comments

  • Dachstein
    Dachstein over 3 years

    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?

  • Dachstein
    Dachstein about 7 years
    cheers! it works. So when I have more config classes in the config package, all of them will automatically get picked up because I added @ComponentScan to one of them in this package?
  • Dachstein
    Dachstein about 7 years
    Also, why does it work with the SpringBootTest annotation out of the box? There is no ComponentScan annotation anywhere needed.
  • AchillesVan
    AchillesVan about 7 years
    I can't tell but i suggeste you to read Spring in action 4th edition chapter 2.2 Automatically wiring beans. You can also read chapter 4 of Spring Boot in Action.
  • Dachstein
    Dachstein about 7 years
    Okay I will get this book. Thanks George!
  • Darren Forsythe
    Darren Forsythe about 7 years
    You may want to be careful as a DataJpaTest is a slice scanning all configurations may introduce unexpected required dependencies if it Enables @Enablething with the configuration. This is why you should avoid putting @Enable on your application class or rolling them to one big configuration.
  • Michael Simons
    Michael Simons over 6 years
    This solution is superior as it is fine grained and doesn't do the opposite of what the slice expresses.
  • I have 10 fingers
    I have 10 fingers about 2 years
    Thank you for your beautiful and clean solution!!