question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataJpaTest fails when updating to SpringBoot 1.5.5.RELEASE

See original GitHub issue

I have a working test against an in-memory database with Spring Boot 1.5.4.RELEASE (or 2.0.0.M2) When I upgrade my project to 1.5.5.RELEASE (or 2.0.0.M3) the test fails because it cannot load the application context:

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages
	at org.springframework.boot.autoconfigure.AutoConfigurationPackages.get(AutoConfigurationPackages.java:78)
	at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.getBasePackages(AbstractRepositoryConfigurationSourceSupport.java:78)
	at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport$1.getBasePackages(AbstractRepositoryConfigurationSourceSupport.java:72)
	at org.springframework.data.repository.config.RepositoryConfigurationSourceSupport.getCandidates(RepositoryConfigurationSourceSupport.java:75)
	at org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport.getRepositoryConfigurations(RepositoryConfigurationExtensionSupport.java:83)
...

Basically, this is my test

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SamePackageEntity.class})
@DataJpaTest
public class SamePackageEntityTest {

    @Autowired
    private SamePackageRepository samePackageRepository;

    @Test
    public void repositoryShouldHaveBeenInitialized() throws Exception {
        assertThat( samePackageRepository.count() ).isEqualTo( 0L );
    }

}

Is the problem in the update or in my Test?

I’ve added a pull request to spring-boot-issues that reproduces this problem

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Dec 1, 2017

and: would you mind to reopen this issue, till it is fixed?

I still don’t believe there’s anything to fix here. Your application fits the description in this comment I made above. Specifically, there’s no class annotated with @EnableAutoConfiguration and you’re trying to use @DataJpaTest. That only worked previously due to a bug in 1.5.4 and earlier.

Replaced @SpringBootTest by @ContextConfiguration

That shouldn’t be necessary, and is rather odd as you’re referencing an entity in your @ContextConfiguration rather than a configuration class. Instead, you should add a main application class annotated with @SpringBootApplication (or @EnableAutoConfiguration) as shown in the following diff:

diff --git a/gh-10465/src/main/java/com/github/akutschera/data/jpatest/JpaTestApplication.java b/gh-10465/src/main/java/com/github/akutschera/data/jpatest/JpaTestApplication.java
new file mode 100644
index 0000000..a1961c8
--- /dev/null
+++ b/gh-10465/src/main/java/com/github/akutschera/data/jpatest/JpaTestApplication.java
@@ -0,0 +1,13 @@
+package com.github.akutschera.data.jpatest;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class JpaTestApplication {
+
+       public static void main(String[] args) throws Exception {
+               SpringApplication.run(JpaTestApplication.class, args);
+       }
+
+}
diff --git a/gh-10465/src/test/java/com/github/akutschera/data/jpatest/SamePackageEntityTest.java b/gh-10465/src/test/java/com/github/akutschera/data/jpatest/SamePackageEntityTest.java
index f3b41da..8c605db 100644
--- a/gh-10465/src/test/java/com/github/akutschera/data/jpatest/SamePackageEntityTest.java
+++ b/gh-10465/src/test/java/com/github/akutschera/data/jpatest/SamePackageEntityTest.java
@@ -11,7 +11,6 @@ import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringRunner;

 @RunWith(SpringRunner.class)
-@ContextConfiguration(classes = {SamePackageEntity.class})
 @DataJpaTest
 public class SamePackageEntityTest {

What would be your recommendation for auto configurations? Create my own one and exclude the other one e.g. JpaBaseConfiguration?

Upon closer inspection, I’ve just noticed that you’re subclassing JpaBaseConfiguration as you want to use Eclipse Link rather than Hibernate. For that purpose, it isn’t discouraged.

1reaction
nenaraabcommented, Nov 30, 2017

@wilkinsona Thanks for the tips! Replaced @SpringBootTest by @ContextConfiguration.

What would be your recommendation for auto configurations? Create my own one and exclude the other one e.g. JpaBaseConfiguration?

and: would you mind to reopen this issue, till it is fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot Reference Guide
This section provides a brief overview of Spring Boot reference documentation. Think of it as map for the rest of the document. You...
Read more >
[Solved]-Hibernate-core error when using spring-boot-starter-data ...
Coding example for the question Hibernate-core error when using spring-boot-starter-data-jpa-Hibernate.
Read more >
Spring Boot Starter Test - Maven Repository
Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito. License, Apache 2.0.
Read more >
Spring Boot changelog - Awesome Java - LibHunt
New Features. Provide a property or environment variable to enable DevTools' restarter irrespective of how the application was launched #21424 ...
Read more >
io.zonky.test:embedded-database-spring-test 2.1.2 on Maven
Supported versions are Spring 4.3.8+ and Spring Boot 1.4.6+ ... If you are upgrading from the 1.x version, check the release notes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found