Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

eclipse - Unit Tests are getting failed in Java 8 to Java 11 Migration

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the directory that contains the "spring" directory is what is in your classpath, and not the "spring" directory itself, then this is not a "workaround", but a "fix". If you're not allowed to change anything, then you can't fix anything either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...