Lets say I have the following setup for 2 test classes in a SpringBoot Project
class Config1 {
@Bean
public MyObject myObject() {
return Mockito.mock(myObject.class);
}
}
class Config2 {
@Bean
public MyObject myObject() {
return new MyObject();
}
}
@ContextConfiguration(classes = {Config1.class, Config2.class})
class Parent {
....
}
class Test1 extends Parent {
@AutoWired
private MyObject myObject; // Instance of MyObject rather than a Mock
....
}
class Config3 extends Config1 {
....
}
@ContextConfiguration(classes = Config3.class)
class Test2 extends Parent {
@AutoWired
private MyObject myObject; // Instance of a Mock rather than MyObject
....
}
As the comments mention, for Test1
the instance of MyObject
comes from Config2
, while Test2
receives the Config3
(inherited via Config1
) mocked instance of MyObject
.
The rule this seems to follow is the last evaluated config wins. i.e. If Parent
was defined as
@ContextConfiguration(classes = {Config2.class, Config1.class})
class Parent {
Then only the Mock would be used (I assume).
Where are the the rules, that define the evaluation order, for the @ContextConfiguration
described?
There appear to be flags to control whether inheritance is ignored. See https://www.logicbig.com/tutorials/spring-framework/spring-core/test-configuration-inheritance.html. But I did not find anything to describe the evaluation order.
PS: Apologies for the convoluted sample code. It's a simplified version of something I came across.