-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@InjectSpy and @InjectMock cause @Valid behaviour change #18388
Comments
A possible solution would be changing public class ArcProxyBeanMetaDataClassNormalizer implements BeanMetaDataClassNormalizer {
@Override
public <T> Class<? super T> normalize(Class<T> beanClass) {
if (!Subclass.class.isAssignableFrom(beanClass)) {
return beanClass;
}
return normalize(beanClass.getSuperclass());
}
} This solves the issue for Spies, but Mocks still behave weirdly. I wonder if that's something we should worry about, though. As per Mockito's documentation, Mocks aren't built for partially mocking an object, specially if the target method throws exceptions, or depends on object state. I believe the highlighted part is what might allow us to leave it as is for Mocks. |
quarkusio#18388 (cherry picked from commit 0c66ba5)
Describe the bug
When using
@io.quarkus.test.junit.mockito.InjectMock
or@io.quarkus.test.junit.mockito.InjectSpy
for a bean that has methods using@javax.validation.Valid
, validations are being skipped.Expected behavior
If
@InjectMock
along with.callRealMethod()
, or if@InjectSpy
are used on a method that has@Valid
, I expect the system to validate the bean accordingly.Actual behavior
The system is running as if no validations were set up, leading to errors and failing tests.
To Reproduce
I built a project to reproduce the problem, so just
mvn test
it.Configuration
It's all in the project.
Environment (please complete the following information):
Output of
uname -a
orver
Output of
java -version
Quarkus version or git rev
Build tool (ie. output of
mvnw --version
orgradlew --version
)Additional context
(Add any other context about the problem here.)
I'm running this through
IntelliJ IDEA 2021.1.3
.Despite the sample project using 2.0.0.Final, the same problem happens on 1.13.7.Final.
From what I could tell, when either
InjectMock
orInjectSpy
are used, the generated subclasses are called in an order that affects the Validation behaviour.More specifically, at
validateParameters(T, java.lang.reflect.Executable, java.lang.Object[], java.lang.Class<?>...)
:At the
rootBeanMetaData.hasConstraints()
check, when using Spies or Mocks, the variable is false.After some debugging, I noticed that this happens because Spies and Mocks cause the use of
org.hibernate.validator.internal.metadata.PredefinedScopeBeanMetaDataManager.UninitializedBeanMetaData.hasConstraints
as the BeanMetadData implementation.Which in turn led me to this:
That comment about child classes seems to be the reason this is happening.
Before I start working on a solution, I was wondering if someone has a word on that.
The text was updated successfully, but these errors were encountered: