Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 337
Dachstein Avatar asked Apr 30 '17 11:04

Dachstein


People also ask

What does SpringBootTest annotation do?

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.

What is the use of @DataJpaTest annotation?

@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.

How do you use TestConfiguration?

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.

How does @SpringBootTest work?

@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.


2 Answers

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 {
}
like image 50
Sydney Avatar answered Sep 28 '22 10:09

Sydney


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 ...
}
like image 44
AchillesVan Avatar answered Sep 28 '22 08:09

AchillesVan