6

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?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 1
    What do you mean by "not allowed to edit the source code"? Have you spoken to the programmers? Have you spoken to your boss about that? – J Fabian Meier Feb 01 '21 at 16:41
  • Exactly, what is meant by not allowed to change the source code ? You are working on migration, right ? Then, in that case, you have to modify the source code. – Anish B. Feb 04 '21 at 12:56
  • Provide the codebase through github possible. – Anish B. Feb 04 '21 at 16:30

2 Answers2

3

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.

David M. Karr
  • 14,317
  • 20
  • 94
  • 199
0

According to your fix, it seems that test-context.xml is now located at classpath*:/spring/test-context.xml. Therefore, you can try adding the spring folder where test-context.xml is into the class path as well, and then it should work. No code changes needed for this solution. You can read how to do it at How do I add a directory to the eclipse classpath?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35