IntelliJ Cant Test Context Search

I am having problems with unit tests in intelliJ. I looked at other forums where people had similar problems, but so far I still have not been able to get it to work. This is mistake:

java.lang.IllegalStateException: Failed to load ApplicationContext ... Caused by: java.io.FileNotFoundException: class path resource [com/d1/d2/service/ServiceTest-context.xml] cannot be opened because it does not exist 

In my test, I:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "ServiceTest-context.xml") public class ServiceImplTest .... 

I obviously confirmed that the file exists where it searches for it in the called line. I also read the suggestion to check the output directory, and I also confirmed that it exists there. Is there anything else that stands out to you? I can run tests from the command line using ant, but I would like to be able to run individual classes, not modules.

Project:

 IntelliJ_project src test com d1 d2 otherStuff ... ... service ServiceImplTest.java ServiceTest-context.xml 

outputFolder:

 test-classes (output folder) com d1 d2 service ServiceImplTest.class 

So it turns out that I was looking for the wrong directory for output. The context file does not get to the output location. How do I get it?

+4
source share
3 answers

If this is a multi-module project, and your tests work when you start with

 mvn test 

but the configuration file was not found by IDEA, try the following:

  • menu: Run β†’ Edit Configurations ...
  • change the "Working Directory" to point to the "Intellij_Project" folder (with a trailing slash).
  • in the test class, change the configuration to:

     @ContextConfiguration("file:test/com/d1/d2/service/ServiceTest-context.xml") 

This configuration worked for me (and also uses spring -mvc-showcase).

+5
source

Try this solution, it works for me:

  • Place ServiceTest-context.xml in the src/test/resources folder.

  • Add these lines to the build section of your pom:

     <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> 
  • Reimport Maven project and do individual tests.

+4
source

With the same problem. An error appears in IntelliJ where it does not copy all the files to the target directory. Therefore, your context was not found. Had the same problem.

If you check the target / test class folder, you may notice that the context is not in the right place. At least what I had.

I had troubles in the past when they were fixed (besides a manual copy). If you're lucky, try marking up test resources and marking this as a test resource. Then make mvn clean. Then run the test. Also, make sure that in your test case config, it runs Make ... This got my context in the test classes. Also check the correct working directory of your test class. I had a case when it was installed in the directory in which I saved the project files ....

0
source

Source: https://habr.com/ru/post/1481761/


All Articles