Why can't my test inherit the ContextConfiguration location from the parent?

In the interest of DRY, I want to define my ContextConfiguration in the parent class and inherit all of my test classes, for example:

Parent class:

package org.my; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/my/Tests-context.xml") public abstract class BaseTest { } 

Child class:

 package org.my; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(inheritLocations = true) public class ChildTest extends BaseTest { @Inject private Foo myFoo; @Test public void myTest() { ... } } 

According to the ContextConfiguration docs, I have to inherit the parent location, but I can't get it to work. Spring is still looking for the file in the default location ( /org/my/ChildTest-context.xml ) and barfs when it cannot find it. I tried the following with no luck:

  • Creating a parent class specific
  • Adding a no-op test to the parent class
  • adding the entered element to the parent class as well
  • combinations of the above

I am in spring -test 3.0.7 and JUnit 4.8.2.

+7
source share
1 answer

Remove @ContextConfiguration(inheritLocations = true) in the child class. inheritLocations is set to true by default.

By adding the @ContextConfiguration(inheritLocations = true) annotation without specifying locations, you tell Spring to expand the list of resource locations by adding a default context, which is /org/my/ChildTest-context.xml .

Try something like this:

 package org.my; @RunWith(SpringJUnit4ClassRunner.class) public class ChildTest extends BaseTest { @Inject private Foo myFoo; @Test public void myTest() { ... } } 
+11
source

All Articles