3

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.

Killer Beast
  • 469
  • 6
  • 21
  • Assuming that your application is using auto-configuration, `@SpringBootTest` should be a superset of `@DataJpaTest`. If there's no entity manager factory it would appear that the JPA auto-configured is not active. I can't tell why from what you've shared thus far. Can you update your question with a [minimal, complete, and verifiable example](/help/mcve)? – Andy Wilkinson Jun 05 '19 at 11:52
  • Why do you think you have to use both SpringBootTest and DataJpaTest? – Simon Martinelli Jun 05 '19 at 12:03
  • @AndyWilkinson we are not using auto-configuration. I will try to make the example but it may take a bit of time. – Killer Beast Jun 05 '19 at 12:06
  • Take a look at this https://stackoverflow.com/a/53932997/6643803, I had a similar issue and that's how I solved it. – akuma8 Jun 05 '19 at 12:22
  • in our project we use the combination of @DataJpaTest with @ContextConfiguration(locations = { "classpath:test-context.xml" }). This is because the testmodule is isolated from all other modules. In the test-context.xml we defined – Devilluminati Jun 05 '19 at 12:41
  • @Devilluminati yes, I switched to a combination of `@ContextConfiguration` and `@TestPropertySource` for me get it running. Someone correct me if its the wrong way to fix this. – Killer Beast Jun 06 '19 at 08:02
  • @Devilluminati if you can post it as an answer, would be glad to accept it and close the question. – Killer Beast Jun 11 '19 at 11:27

1 Answers1

1

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" />
Devilluminati
  • 328
  • 1
  • 15