Recommended way to remove @DataJpaTest when used along with @SpringBootTest

Viewed 818

We have an application that relies on Spring Boot 2.0. We are in the process of migrating it to JDK11 from JDK8. This also enabled us to update Spring Boot from 2.0 to 2.1. After reading through the changelog, it appeared there was any major change that needed for us.

Now the problem lies in where some test classes are annotated with both @SpringBootTest and @DataJpaTest. As per this and as well as the documentation, we are not supposed to use both together and instead we changed @DataJpaTest to @AutoConfigureTestDatabase. Here is how the code is:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {A.class, B.class}, properties = {
    "x=xxx",
    "y=yyy"
})
@AutoConfigureTestDatabase  // Used to be @DataJpaTest
@EnableJpaRepositories("com.test")
@EntityScan("com.test")
public class Test {

    @TestConfiguration
    public static class TestConfig {
        // Some beans returning
    }
    // Tests
}

Now, we end up with the following error:

NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

So as per this answer, we did something like this:

@EnableJpaRepositories(basePackages="com.test", entityManagerFactoryRef="entityManagerFactory")

Even after this we still end up with the same error. Is this the right way to remove @DataJpaTest? Or do we need to remove @SpringBootTest and do something else? Any sort of guidance is much appreciated.

1 Answers

The testclass is annotated with @DataJpaTest and @ContextConfiguration

@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public abstract class AbstractTestCase {

    protected static final Logger LOG = LoggerFactory.getLogger(AbstractTestCase.class);

}

We defined a test-context.xml. This is because the testmodule is isolated from all other modules (multi maven module project). In the test-context.xml we defined the component-scan for the base-package.

<context:component-scan base-package="de.example.base.package" />
Related