Spring unit test - using @ContextConfiguration when extending a class in another project

I have a spring unit test that extends another test located in another project (common for tests). My context file (my-context.xml) is in the same package as MyTest. The base context file ("base-context.xml") is in the same package as BaseTest. The files look like this:

@ContextConfiguration(locations = { "my-context.xml"}) public class MyTest extends BaseTest { @ContextConfiguration(locations = { "base-context.xml" }) public abstract class BaseTest extends AbstractJUnit4SpringContextTests{ 

Problem: BaseTest finds "base-context.xml", but MyTest receives a FileNotFoundException for "my-context.xml". If I move the BaseTest and my-context.xml files to the same package as BaseTest, everything works fine. What could be the problem?

+4
source share
2 answers

Check this out in the spring documentation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-app-ctx

I would add the keyword "classpath:" before base-context.xml and define the path relative to the class path. You should also put your xml files in the src / main / resources subdirectory, and not next to the java files.

+3
source

As mentioned below, you can use the classpath keyword as follows:

 @ContextConfiguration(locations = { "classpath:**/base-context.xml" }) 
0
source

All Articles