Package org.jboss.weld.junit5.auto
Annotation Type ExcludeBean
@Retention(RUNTIME)
@Target({FIELD,METHOD,ANNOTATION_TYPE})
@Inherited
public @interface ExcludeBean
@ExcludeBean excludes a bean, or multiple beans, that include a bean defining annotation
(e.g. scope) from automatic discovery. This can be helpful to allow replacing a bean class with a different
implementation, typically a mock.
The type of bean to exclude is implied by the annotated fields' type or annotated methods' return type. If the type is a base class or interface all beans extending / implementing that type will be excluded.
NOTE: This annotation will only exclude beans defined by class annotations. It will not exclude beans of the
implied type that are defined by Produces producer methods / fields or synthetic
beans. Also, current implementation excludes beans based on type, disregarding any qualifiers that are specified.
Example:
@EnableAutoWeld
class TestSomeFoo {
@Inject
SomeFoo someFoo; // SomeFoo depends upon application scoped bean Foo
@Produces
@ExcludeBean // Excludes beans with type Foo from automatic discovery
Foo mockFoo = mock(Foo.class); // mockFoo is now produced in place of original Foo impl
@Test
void test(Foo myFoo) {
assertNotNull(myFoo);
assertEquals(myFoo.getBar(), "mock-foo");
}
}
- See Also: