When using the audit-logging
plugin for Grails 4 a NoSuchBeanDefinitionException
is thrown when accessing a domain properties
collection in unit tests.
Domain unit tests are failing due to a missing bean, which should not be required.
This is required for two methods, getLogURI
and getLogCurrentUserName
as they
require the AuditRequestResolver
bean.
The error is:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'grails.plugins.orm.auditable.resolvers.AuditRequestResolver' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:339)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
at grails.plugins.orm.auditable.Auditable$Trait$Helper.getLogCurrentUserName(Auditable.groovy:111)
at groovy.lang.MetaBeanProperty.getProperty(MetaBeanProperty.java:59)
at grails.beans.util.LazyMetaPropertyMap.get(LazyMetaPropertyMap.java:126)
at audit.bean.in.tests.Vehicle.clone(Vehicle.groovy:16)
at audit.bean.in.tests.VehicleSpec.Test cloning method(VehicleSpec.groovy:26)
./gradlew test
this project
It is possible to work around the bean requirement by defining the bean per test class:
@Override
Closure doWithSpring() {{->
auditRequestResolver(DefaultAuditRequestResolver)
}}
See commented out code in VehicleSpec
Explicitly removing the properties from the clone candidates also works around the issue as it does not call the methods in question
propertiesToClone.remove("logCurrentUserName")
propertiesToClone.remove("logURI")
See commented out code in Vehicle
Running the workarounds:
- Uncomment the
doWithSpring
block in the tests ./gradlew test
for a green run
By modifying the methods above the exception can be either caught or prevented. See both proposed options in AuditableModified
Running the fixes:
- Comment out
doWithSpring
changes from the above workaround - Change the trait used for
Vehicle
(change the commented out line) ./gradlew test
for a green run