Annotation Interface ReplaceInRegistry
@Documented
@Inherited
@Retention(RUNTIME)
@Target({METHOD,FIELD})
public @interface ReplaceInRegistry
@ReplaceInRegistry is an annotation used to mark all the methods and fields whose return value or value
should replace an existing bean in the registry. It is meant to be used to replace a real implementation of a service
with a mock or a test implementation.
If a field is marked with the annotation @ReplaceInRegistry, the name and the type of the field are used to
identify the bean to replace, and the value of the field is the new value of the bean. The field can be in the test
class or in a parent class.
In the next example, the annotation ReplaceInRegistry on the field myGreetings of type
Greetings indicates that the bean with the same name and type should be replaced by an instance of
CustomGreetings.
@CamelMainTest
class SomeTest {
@ReplaceInRegistry
Greetings myGreetings = new CustomGreetings("Willy");
// The rest of the test class
}
If a method is marked with the annotation @ReplaceInRegistry, the name and the return type of the method are
used to identify the bean to replace, and the return value of the method is the new value of the bean. The method can
be in the test class or in a parent class.
In the next example, the annotation ReplaceInRegistry on the method myGreetings whose return type is
Greetings indicates that the bean with the same name and type should be replaced by an instance of
CustomGreetings.
@CamelMainTest
class SomeTest {
@PropertyInject("name")
String name;
@ReplaceInRegistry
Greetings myGreetings() {
return new CustomGreetings(name);
}
// The rest of the test class
}
This annotation can be used in @Nested test classes. The @ReplaceInRegistry annotations of outer
classes are processed before the @ReplaceInRegistry annotations of inner classes.