I'm working on migrating my project from Java 8 to Java 11. So I used, Spring 5.1.0, Java 11, Eclipse 4.16 and Tomcat 9. I'm able to build the source successfully. But when it comes to Tests, they are getting failed.
Here is what I've in pom.xml for tests.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
And my test cases runs absolutely fine with the above dependencies in Java 8. But when I migrate the code to Java 11, I'm getting the below exception.
ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing
TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@54e063d] to
prepare test instance [com.test.SomeTest2@4293943]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DefaultTestCDependencyInjectionTestExecutionListenerontext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DefaDefaultTestCDependencyInjectionTestExecutionListenerontextultTestContext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
Sample Test Class Structure I've
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class SomeTestClass {
...
}
Which is getting failed because of the mentioned exception. But, I did some research and found a workaround i.e
to change from:
@ContextConfiguration(locations = {"classpath:test-context.xml"})
to this:
@ContextConfiguration(locations = {"classpath*:/spring/test-context.xml"})
And it works. But the problem is that I'm not allowed to edit the source code. How to achieve it?
question from:
https://stackoverflow.com/questions/65937262/unit-tests-are-getting-failed-in-java-8-to-java-11-migration 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…