20

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
  • 3,994
  • 6
  • 34
  • 61

3 Answers3

24

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 {
}
Sydney
  • 11,964
  • 19
  • 90
  • 142
10

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 ...
}
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • 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 Apr 30 '17 at 12:05
  • Also, why does it work with the SpringBootTest annotation out of the box? There is no ComponentScan annotation anywhere needed. – Dachstein Apr 30 '17 at 12:08
  • 1
    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. – AchillesVan Apr 30 '17 at 12:10
  • Okay I will get this book. Thanks George! – Dachstein Apr 30 '17 at 12:12
-1

After @georges van post I have found out that ALL configuration classes get also picked up by just adding one line in the test:

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

    // Tests ...
}

If someone only wants ONE specific configuration class you can do:

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

    // Tests ...
}

Or multiple classes with:

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

Dachstein
  • 3,994
  • 6
  • 34
  • 61
  • 1
    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. – Darren Forsythe Apr 30 '17 at 14:29