21
votes

I have a problem with maven and JUnit testing.

I have some files in src/main/resources and when running junit tests I want to these files. Only one test specific file with slightly different settings should overrule a corresponding file. So my idea was to give this test-file the same name like the main-file and put it file under src/test/resources at the same (corresponding) place like the main-file. But now I have the problem, that I cant use all the other files from src/main/resources.

I thought they junit test would coppy them default into target/test-classes/ when running ernriced by the files from src/test/resources, but it doesnt. There is only the file from src/test/resources and not other.

Thanks for any ideas, how I could solf this problem.

here an example how I try to access the files

'

@BeforeClass
public static void globalSetUp() throws NamingException, SQLException {
    System.setProperty("solr.solr.home", "/solr/");

    cores = new CoreContainer(
            "/home/foo/workspace/reporting/target/test-classes/solr");
    cores.load();
    server = new EmbeddedSolrServer(cores, "reporting");

    loadDriver();
    connection = createAndConnectToDB();
    createDBSchema();

}

'

2
You still have not given us enough information. Try posting code to show how you are trying to load these files.John B
I edited my inital post, showing how I refer to the files in src/main/resources. Thanks for your thoghts!MrLang
I do not see anything that seems to be referring to files in /src/main/resourcesJohn B
Ok, but I thought the files from /src/main/resources will be copied to /target/test-classes when running a junit test.MrLang
You should load them from "src/main/resources/myFile" or if using a classpath loader "classpath:myFile"John B

2 Answers

27
votes

The problem was in pom.xml. /main/resources was not as testResource.

Here's the snippet that solved my problem:

<build>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
      <testResource>
        <directory>${project.basedir}/src/main/resources</directory>
      </testResource>
    </testResources>
</build>
10
votes

Actually both are there / available. The files in src/main/resources and src/test/resources. However when loading files from the classpath, any file in src/test/resources takes precedence over the files in src/main/resources.

It it difficult to fully answer your question since you did not provide a lot of details (are you using ContextConfiguration? If not how are you loading the files) but I would suggest that you use a different name and load that file explicitly in the one test that needs it.

You could also have the same name but then all other tests would need to explicitly use the src/main/resources path to load the default file.