Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring does a ContextConfiguration(...) inherit from its parent @ContextConfiguration?

Suppose I have a parent test class like so:

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyCustomTestConfig.class })
public class MyParentTestclass {

Then I have a child class where I want to add the Spring 3.2.3 name annotation attribute.

@ContextConfiguration(name=MyName)
public class MyChildTestClass extends MyParentTestClass {

I still want to get all the context configuration from the parent - but am not sure if it will come through.

My question is: In Spring does a ContextConfiguration(...) inherit from its parent @ContextConfiguration?

like image 714
hawkeye Avatar asked Mar 24 '16 00:03

hawkeye


1 Answers

The @ContextConfiguration does support inheritance out of the box.

@ContextConfiguration has a property called inheritLocations, which defaults to true and indicates whether or not resource locations or annotated classes from test superclasses should be inherited.

inheritLocations = true: This means that an annotated class will inherit the resource locations or annotated classes defined by test superclasses. Specifically, the resource locations or annotated classes for a given test class will be appended to the list of resource locations or annotated classes defined by test superclasses. Thus, subclasses have the option of extending the list of resource locations or annotated classes.

If inheritLocations is set to false, the resource locations or annotated classes for the annotated class will shadow and effectively replace any resource locations or annotated classes defined by superclasses.

In the following example that uses annotated classes, the ApplicationContext for ExtendedTest will be loaded from the BaseConfig and ExtendedConfig configuration classes, in that order. Beans defined in ExtendedConfig may therefore override those defined in BaseConfig.

 @ContextConfiguration(classes=BaseConfig.class)
 public class BaseTest {
     // ...
 }

 @ContextConfiguration(classes=ExtendedConfig.class)
 public class ExtendedTest extends BaseTest {
     // ...
 }
like image 89
pczeus Avatar answered Sep 21 '22 16:09

pczeus