jcabi / jcabi-aspects

Collection of AspectJ Java Aspects to facilitate aspect-oriented programming patterns: logging, caching, validating, etc.
https://aspects.jcabi.com
Other
528 stars 150 forks source link

javax.validation.ConstraintViolationException: may not be null for a mocked object in TestNG test #234

Open sandeepkhanzode opened 7 years ago

sandeepkhanzode commented 7 years ago

I started to integrate jcabi-aspects into an already built Spring application with JSR-303 Hibernate-Validator based annotations. The @Loggable seems to work displaying the time, etc. But the existing TestNG test cases fail at weird code locations. Weird since the @NotNull annotation should not fail because a properly mocked instance of the argument is being passed in the test. I can verify this by removing the ajc plugin from the build->plugins in pom.xml and running the test. Of course, everything works properly.

I am not sure why the MethodValidator of jcabi is being invoked at the stack trace below. Please let me know what I should check. Thanks!!

I am using jcabi-aspects v.0.22.5 and aspectjrt v.1.5.4

**javax.validation.ConstraintViolationException: may not be null**
    at com.jcabi.aspects.aj.MethodValidator.checkForViolations(MethodValidator.java:189)
    at com.jcabi.aspects.aj.MethodValidator.validateMethod(MethodValidator.java:154)
    at com.jcabi.aspects.aj.MethodValidator.beforeMethod(MethodValidator.java:87)
    at ...

I also see this in the logs for init JMockit mocks ...

---

FAILED CONFIGURATION: @BeforeMethod xxx
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:

-> at ....
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods _cannot_ be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
dmarkov commented 7 years ago

@yegor256 please pay attention to this issue (par.21)

yegor256 commented 7 years ago

@sandeepkhanzode well, if you pass NULL and there is @NotNull annotation, you get an exception. That's what jcabi-aspects is doing.

naveensharma18 commented 6 years ago

I'm not sending null, still getting same exception(may not be empty). Below is the method argument

@NotBlank @FormParam(MOBILE_PHONE_NUMBER) String mobilePhoneNumber