Details
Description
When the list supplied to GrailsUnitTestCase's mockDomain method contains domain objects that are of a subtype of the mocked domain class, their meta-classes are enhanced (e.g. dynamic finders are added), but registerMetaClass is never called for those subclasses. This results in unexpected behaviour in integration tests, where the dynamic methods are expected to run 'normally', i.e. against the in-memory database. The attached project shows this: grails test-app :integration works, whereas grails test-app doesn't, because the unit test uses mockDomain.
A possible fix would be to replace the first line of mockDomain (registerMetaClass(domainClass)) with
([domainClass] + instances.collect {it.class}).unique().each {domainClassToRegister ->
registerMetaClass(domainClassToRegister)
}
-
Hide
- test.zip
- 12/Apr/11 7:39 AM
- 186 kB
- Levi Hoogenberg
-
- test/.classpath 0.7 kB
- test/.project 0.5 kB
- test/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- test/application.properties 0.2 kB
- test/grails-app/conf/BootStrap.groovy 0.1 kB
- test/grails-app/conf/BuildConfig.groovy 1 kB
- test/grails-app/conf/Config.groovy 4 kB
- test/grails-app/conf/DataSource.groovy 0.8 kB
- test/grails-app/.../spring/resources.groovy 0.0 kB
- test/grails-app/conf/UrlMappings.groovy 0.2 kB
- test/grails-app/domain/test/Book.groovy 0.1 kB
- test/grails-app/domain/test/Item.groovy 0.1 kB
- test/grails-app/i18n/messages.properties 3 kB
- test/grails-app/.../messages_da.properties 3 kB
- test/grails-app/.../messages_de.properties 4 kB
- test/grails-app/.../messages_es.properties 3 kB
- test/grails-app/.../messages_fr.properties 2 kB
- test/grails-app/.../messages_it.properties 2 kB
- test/grails-app/.../messages_ja.properties 2 kB
- test/grails-app/.../messages_nl.properties 3 kB
- test/.../messages_pt_BR.properties 3 kB
- test/.../messages_pt_PT.properties 3 kB
- test/grails-app/.../messages_ru.properties 4 kB
- test/grails-app/.../messages_th.properties 5 kB
- test/.../messages_zh_CN.properties 2 kB
- test/grails-app/views/error.gsp 2 kB
- test/grails-app/views/index.gsp 4 kB
- test/grails-app/views/layouts/main.gsp 0.8 kB
- test/stacktrace.log 277 kB
- test/target/.../TESTS-TestSuites.xml 1 kB
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
This is fixed in 2.0 where we have a new MetaClassRegistryCleaner that eliminates the need for registerMetaClass
Ouch! This bit me hard in the ass. The integration tests would fail with error,
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: SomeClass.InMyProject
Which might have just said "Something is wrong somewhere, good luck with that...." Took me about five hours to reduce it to a simple "registerMetaClass(...)" on each of my subclasses. (Not entirely because I'm especially dull, but in part because I was adding in a bunch of unit tests which worked fine alone but would break with the integration tests. I had to do a binary search on what was breaking the integration run. The integration run takes 3 minutes to run.)