jensengar
jensengar

Reputation: 6167

IntelliJ Cant Find Test Context

I am having issues running unit tests in intelliJ. I have looked through other forums where people have had similar issues but so far I still haven't been able to get it to work. This is the error:

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 have:

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

I have obviously verified that the file exists where it is looking for it in the caused by line. I also read a suggestion to check the output directory and I have verified that it exists there as well. Is there anything else that stands out to you? I can run the tests from the command line using ant but I would like to be able to run individual classes rather than 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 I was looking in the wrong directory for the output. The context file does not make it to the output location. How do I get it there?

Upvotes: 5

Views: 11073

Answers (3)

Lawrence
Lawrence

Reputation: 1056

Having the same issue. There appears to be a bug in IntelliJ where it does not copy all files to the target dir. Hence, your context is not found. Had the same issue.

If you check your target/test-classes folder, you may notice the context is not in the desired place. At least that's what I had.

I had lotsa troubles in the past getting it right (besides a manual copy). If you're lucky, try unmarking the test resources and remark it as test resource. Then do a mvn clean. Then run your test. Also, make sure in your test case config, it triggers a Make... That got my context in the test classes. Also verify if the Working directory of your test class is correct. I had a case where it was set to the dir in which I kept the project files....

Upvotes: 0

Michał Rybak
Michał Rybak

Reputation: 8706

If this is a multi-module project and your tests do work when run with

mvn test

but config file can't be found by IDEA, try this:

  1. menu: Run -> Edit Configurations...
  2. change "Working Directory" to point at your "Intellij_Project" folder (with a trailing slash).
  3. in test class, change your configuration to:

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

This configuration worked for me (and is also used by spring-mvc-showcase).

Upvotes: 6

Eugene Evdokimov
Eugene Evdokimov

Reputation: 2545

Try this solution, it works for me:

  1. Put ServiceTest-context.xml into the src/test/resources folder.

  2. Add these lines to the build section of your pom:

    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
    
  3. Reimport Maven project and run your individual tests.

Upvotes: 4

Related Questions