Reputation: 1386
We have an application that uses javax.enterprise.inject.Instance to find all validators for a service. This seems to work nicely but during testing we would like to swap out a few of those validators with a mock to skip their behaviour so it's easier to reach other validators.
When we try to do this using the 'Old approach' (See Blog-post on this) we notice only the classes with @Mock are injected, not any of the normal ones.
When trying it the 'New approach' we get a javax.enterprise.inject.AmbiguousResolutionException that tells us TypeBValidator is found twice.
This is the setup of a trimmed-down version of our app showing the behaviour:
@ApplicationScoped
)@ApplicationScoped
)Instance<Validator> validators
via the constructor.In test-scope we have:
@QuarkusTest
. This test-class calls the REST-service using restassured, right as it's provided when one generates an example project.See the example project for all details.
I'm looking for a stable and predictable way to replace only the TypeBValidator during my tests. Any help would be highly appreciated.
Upvotes: 0
Views: 2045
Reputation: 64011
Turns out that this was not a bug, but the expected behavior of CDI when multiple there is a class hierarchy where multiple classes are beans.
In this case, the simplest solution is to annotate TypeBValidator
with @DefaultBean
.
See https://github.com/quarkusio/quarkus/issues/19773#issuecomment-909974623 for more details
Upvotes: 1