jcabi-aspects
jcabi-aspects copied to clipboard
JSR-303 Validation test failed after adding jcabi-aspects
Hi, I am using jcabi-aspects to record the execution time of methods. Based on some search I also added the hibernate-validator in the dependency:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
Now after running "mvn clean install" I get below error:
#
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapter]: Factory method 'getMtsoMessageServiceAdapter' threw exception; nested exception is javax.validation.ConstraintViolationException: may not be null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 56 more
Caused by: javax.validation.ConstraintViolationException: may not be null
at com.jcabi.aspects.aj.MethodValidator.checkForViolations(MethodValidator.java:189)
at com.jcabi.aspects.aj.MethodValidator.validateConstructor(MethodValidator.java:171)
at com.jcabi.aspects.aj.MethodValidator.beforeCtor(MethodValidator.java:111)
at com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapter.<init>(MtsoMessageServiceAdapter.java:37)
at com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapterValidationTest$Config.getMtsoMessageServiceAdapter(MtsoMessageServiceAdapterValidationTest.java:123)
at com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapterValidationTest$Config$$EnhancerBySpringCGLIB$$8684d275.CGLIB$getMtsoMessageServiceAdapter$0(<generated>)
at com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapterValidationTest$Config$$EnhancerBySpringCGLIB$$8684d275$$FastClassBySpringCGLIB$$bb124cd1.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355)
at com.nuance.him.utmc.persistence.services.service.employee.adapter.MtsoMessageServiceAdapterValidationTest$Config$$EnhancerBySpringCGLIB$$8684d275.getMtsoMessageServiceAdapter(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
# ... 57 more
This bean mtsoMessageServiceAdapter is returned from a static inner class written in the test class
@Configuration
@Import(value = CommonConfig.class)
static class Config {
/**
* Create {@link MtsoMessageServiceAdapter} bean for test.
*
* @return {@link MtsoMessageServiceAdapter}
*/
@Bean
public MtsoMessageServiceAdapter getMtsoMessageServiceAdapter() {
return new MtsoMessageServiceAdapter(mtsoMessagePersistenceService);
}
}
Above mtsoMessagePersistenceService is a mocked object:
@Mock
private static MtsoMessagePersistenceService mtsoMessagePersistenceService;
@yegor256 dispatch this issue please, see par.21
Adding more details. I am able to use the jcabi-aspects library. Problem is in running the testNG unit tests. For some of unit tests I can see the logs with method execution time.
For example below unit test:
@Test(expectedExceptions = DataAccessException.class)
public void testCreateItemsForDataAccessException() throws Exception {
when(mockNamedParameterJdbcDaoSupport.getNamedParameterJdbcTemplate().batchUpdate(anyString(), any(SqlParameterSource[].class))).thenThrow(mockedDataAccessException);
mockTimeOffItemDao.create(EMPLOYEE_ID, Collections.emptyList());
}
In above code all objects starting with mock are mocked instance objects. It gives below error:
FAILED: testCreateItemsForDataAccessException
javax.validation.ConstraintViolationException: size must be between 1 and 2147483647
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 com.nuance.him.utmc.datamanager.employee.dal.SpringTimeOffItemDao.create(SpringTimeOffItemDao.java:72)
at com.nuance.him.utmc.datamanager.employee.dal.SpringTimeOffItemDaoNegativeTest.testCreateItemsForDataAccessException(SpringTimeOffItemDaoNegativeTest.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
The issue is where I add Collections.emptyList() for method call. The method has validation for that parameter to be not null and minimum size 1.
As these beans are not created with Spring, so I do not expect the validation should be triggered. These tests were running without error with aspectJ library.
Any idea how to stop the validation for unit tests with jcabi-aspects?