26

I've tried to load the spring config file in src/test/resources classpath with the following abstract class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext.xml"})
public class BaseIntegrationTests {

}

I have the applicationContext.xml file in src/test/resources but spring cant load it.

Thank you.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
robinmag
  • 17,520
  • 19
  • 54
  • 55

7 Answers7

26

To be precise, it's the content of the test output directory (target/test-classes) that is on the class path, not src/test/resources. But resources under src/test/resources are copied to the test output directory by the resources:testResources goal (which is bound by default to the process-test-resources phase).

Having that said, your code looks fine and resources for the test source code should have been copied either by your IDE or by Maven when running tests and should thus be available on the class path. So there must be something else wrong. I can see that your class is a base class for integration tests. Did you configure anything fancy in your pom? Can you show it?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    process-test-resources helped. Fixed the pom. Thank you! – Azee Dec 04 '13 at 15:12
  • my resource is on the test-classes directory and i can load the file with `Myclass.class.getClassLoader.getResource("/my/path")`. but doesn't work with `@ContextConfiguration(location = "classpath:/my/path")` – herau Jun 13 '14 at 08:04
16

Try by using * so that it can search to your classpath

@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
prashant thakre
  • 5,061
  • 3
  • 26
  • 39
  • 4
    What does the `*` do compared to `{"classpath:applicationContext.xml"` ?Search any depth of the classpath? I can't find the documentation explaining this. – markdsievers Aug 10 '16 at 20:49
  • 1
    @markdsievers It merges multiple files into one instead of picking the first one found. See: https://stackoverflow.com/a/3294506/344480 – Matthias Oct 20 '17 at 12:28
6

There is a reported bug with using the spring-test dependency (includes SpringJUnit4ClassRunner) with versions of JUnit > 4.4.

If you are using a version of JUnit newer than 4.4, trying moving that down to 4.4 and see if it solves your problem.

PROrock
  • 1,040
  • 9
  • 8
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
4

your application context must be included in classpath and put * :

@ContextConfiguration(locations = { "classpath:*/application-context.xml" })
borchvm
  • 3,533
  • 16
  • 44
  • 45
2

You seem to be using maven, and trying to run the tests from within eclipse. Check the buil folder (target/test-classes/) for applicationContext.xml. If it is not there, you'd have to build first.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

I think i have a simillar problem, I found out that my application-context.xml was not on target/test-classes/ neighter on src/test/resources

benzen
  • 6,204
  • 4
  • 25
  • 37
0

If you are using Maven and running test cases from eclipse, project right click > Maven > maven update (ALTF5)might work for you.

msanford
  • 11,803
  • 11
  • 66
  • 93
Rupali
  • 1