Details
Description
The problem appears in a use case of testing a Service. My service uses the JSON converter. The service test is created with MockFor. When launched individually, the test fails because of GRAILS-8534 (converters not initialized when MockFor of services). When launched after running the other tests (including a controller) the test succeeds.
grails
test-app unit: testing.MyService -> KO
test-app unit: grailstest.UserControllerTests
test-app unit: testing.MyService -> OK
Example attached.
-
Hide
- grailstest-bug-report-22122011.zip
- 22/Dec/11 7:54 AM
- 24 kB
- Julien Faissolle
-
- grails-app/.../ApplicationResources.groovy 0.1 kB
- grails-app/conf/BootStrap.groovy 0.1 kB
- grails-app/conf/BuildConfig.groovy 2 kB
- grails-app/conf/Config.groovy 4 kB
- grails-app/conf/DataSource.groovy 1 kB
- grails-app/conf/UrlMappings.groovy 0.2 kB
- grails-app/conf/spring/resources.groovy 0.0 kB
- grails-app/.../UserController.groovy 0.1 kB
- grails-app/domain/grailstest/User.groovy 0.1 kB
- grails-app/i18n/messages.properties 3 kB
- grails-app/.../messages_cs_CZ.properties 3 kB
- grails-app/i18n/messages_da.properties 3 kB
- grails-app/i18n/messages_de.properties 4 kB
- grails-app/i18n/messages_es.properties 3 kB
- grails-app/i18n/messages_fr.properties 2 kB
- grails-app/i18n/messages_it.properties 2 kB
- grails-app/i18n/messages_ja.properties 4 kB
- grails-app/i18n/messages_nl.properties 3 kB
- grails-app/.../messages_pt_BR.properties 3 kB
- grails-app/.../messages_pt_PT.properties 3 kB
- grails-app/i18n/messages_ru.properties 4 kB
- grails-app/i18n/messages_sv.properties 3 kB
- grails-app/i18n/messages_th.properties 6 kB
- grails-app/.../messages_zh_CN.properties 2 kB
- grails-app/.../.MyService.groovy.swp 12 kB
- grails-app/services/.../MyService.groovy 0.1 kB
- grails-app/views/error.gsp 0.3 kB
- grails-app/views/index.gsp 3 kB
- grails-app/views/layouts/main.gsp 2 kB
- test/unit/.../UserControllerTests.groovy 0.3 kB
-
Hide
- mockFor-bug-report-22052013.zip
- 22/May/13 12:32 PM
- 25 kB
- Aaron Long
-
- grails-app/.../ApplicationResources.groovy 0.1 kB
- grails-app/conf/BootStrap.groovy 0.1 kB
- grails-app/conf/BuildConfig.groovy 2 kB
- grails-app/conf/Config.groovy 4 kB
- grails-app/conf/DataSource.groovy 1 kB
- grails-app/conf/UrlMappings.groovy 0.2 kB
- grails-app/conf/spring/resources.groovy 0.0 kB
- grails-app/i18n/messages.properties 3 kB
- grails-app/.../messages_cs_CZ.properties 3 kB
- grails-app/i18n/messages_da.properties 3 kB
- grails-app/i18n/messages_de.properties 4 kB
- grails-app/i18n/messages_es.properties 3 kB
- grails-app/i18n/messages_fr.properties 2 kB
- grails-app/i18n/messages_it.properties 3 kB
- grails-app/i18n/messages_ja.properties 4 kB
- grails-app/i18n/messages_nb.properties 3 kB
- grails-app/i18n/messages_nl.properties 3 kB
- grails-app/i18n/messages_pl.properties 4 kB
- grails-app/.../messages_pt_BR.properties 3 kB
- grails-app/.../messages_pt_PT.properties 3 kB
- grails-app/i18n/messages_ru.properties 4 kB
- grails-app/i18n/messages_sv.properties 3 kB
- grails-app/i18n/messages_th.properties 6 kB
- grails-app/.../messages_zh_CN.properties 2 kB
- grails-app/views/error.gsp 0.5 kB
- grails-app/views/index.gsp 3 kB
- grails-app/views/layouts/main.gsp 2 kB
- src/groovy/test/Thing.groovy 0.1 kB
- test/unit/test/MoreThingTests.groovy 0.4 kB
- test/unit/test/ThingTests.groovy 0.6 kB
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
Just ran across this issue in our unit testing with Grails 2.2.2. We have a service which creates a new Thing() and need to use the MockFor.use() style to intercept construction. Later on, in a test that actually needs to create a real Thing(), it uses the proxy instead.
I managed to fix this by having the MetaClassRegistryCleaner ignore cleaning up anything that is an instanceof MockProxyMetaClass. I think the problem is the use() method is make metaClass changes and then the cleaner is restoring the metaClass to the proxy instead of the adaptee.
I added the second condition on the first if:
private void updateMetaClassOfClass(MetaClass oldMetaClass, Class classToUpdate) { if (oldMetaClass != null && !(oldMetaClass instanceof MockProxyMetaClass)) { Object current = alteredClasses.get(classToUpdate); if (current == null ) { alteredClasses.put(classToUpdate, oldMetaClass); } } else { alteredClasses.put(classToUpdate, NO_CUSTOM_METACLASS); } }
This fixes the problem for my scenario. I also ran our full suite of unit tests and a full "gradle build" in grails-core on the 2.2.x branch with no failures.
I had trouble getting the attached example to reproduce the problem and fix. The "mockFor" bug report has two tests, Thing and MoreThing. If you run Thing tests first in 2.2.2, MoreThing will fail with an expectation error.
grails test-app unit: Thing MoreThing
The test passes with the pull request below. I'm not sure how to unit test the RegistryCleaner in grails-core or I would've submitted some tests.
I can confirm this problem. I use multiple MockFor in different test methods and only the first one is working properly.
Grails version: 2.1.0