Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 2.0 final
-
Fix Version/s: 2.2
-
Component/s: Testing
-
Environment:Hidejava version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)
Distributor ID: Ubuntu
Description: Ubuntu 11.10
Release: 11.10
Codename: oneiricShowjava version "1.6.0_20" Java(TM) SE Runtime Environment (build 1.6.0_20-b02) Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode) Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric
-
Testcase included:yes
Description
When running unit tests there doesn't appear to be a consistent (or working) mechanism to apply mixins to JDK classes (and possibly others). Our current project uses several JDK classes that have been mixed-in with other helper methods (example: Date.mixin DateUtils). Domain, service and controller code depends on having these methods available. When testing these methods aren't available even though they have been mixed in.
The attached bug report contains one domain class and one test which fails when run because Date doesn't contain the monster method that was mixed in with a static initialization block in the same test, see below. I've also tried to mix it in using a @BeforeClass method which appears to work for the first test but all subsequent tests in the same class fail with a 'no method called monster' exception. Using a @Before method works by re-mixing-in DateUtils every time but there appears to be a larger problem. From what I can tell DateUtils is being correctly mixed into Date with a static initializer prior to the MetaClassRegistryCleaner being registered, but for some reason Date doesn't contain the monster method in the test.
@TestFor(Truck)
class TruckTests {
static {
Date.mixin bug2.DateUtils
}
void testBad() {
assert "Monster" == new Date().monster()
}
}
On Grails 1.3.7 we would place references to utility classes that are mixed in with JDK classes into Config.groovy so we knew they would always be there regardless of which environment or which class was loaded first. We wanted to ensure that by the time the project created it's first Date object (via hibernate etc...) that the utility class was already mixed in. In the attached example I didn't do that for simplicity. It doesn't seem to make a difference with the exception of Config.groovy being loaded after the MetaClassRegistryCleaner as been registered.
Mailing list discussion: http://grails.1312388.n4.nabble.com/Grails-2-0-Global-Mixins-Broken-tt4259490.html#none
-
Hide
- bug2-bug-report-06012012.zip
- 06/Jan/12 12:15 PM
- 22 kB
- Stan Carney
-
- 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/domain/bug2/Truck.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/views/error.gsp 0.3 kB
- grails-app/views/index.gsp 3 kB
- grails-app/views/layouts/main.gsp 2 kB
- src/groovy/bug2/DateUtils.groovy 0.1 kB
- test/unit/bug2/TruckTests.groovy 0.2 kB
- application.properties 0.1 kB
-
Hide
- mixinUnitTest.zip
- 08/Jan/12 9:57 PM
- 240 kB
- John Engelman
-
- mixinUnitTest/.classpath 0.7 kB
- mixinUnitTest/.project 0.5 kB
- mixinUnitTest/.../org.codehaus.groovy.eclipse.preferences.prefs 0.1 kB
- mixinUnitTest/application.properties 0.2 kB
- mixinUnitTest/.../ApplicationResources.groovy 0.1 kB
- mixinUnitTest/.../BootStrap.groovy 0.1 kB
- mixinUnitTest/.../BuildConfig.groovy 2 kB
- mixinUnitTest/grails-app/.../Config.groovy 4 kB
- mixinUnitTest/.../DataSource.groovy 1 kB
- mixinUnitTest/.../resources.groovy 0.0 kB
- mixinUnitTest/.../UrlMappings.groovy 0.2 kB
- mixinUnitTest/grails-app/.../Foo.groovy 0.1 kB
- mixinUnitTest/.../messages.properties 3 kB
- mixinUnitTest/.../messages_cs_CZ.properties 3 kB
- mixinUnitTest/.../messages_da.properties 3 kB
- mixinUnitTest/.../messages_de.properties 4 kB
- mixinUnitTest/.../messages_es.properties 3 kB
- mixinUnitTest/.../messages_fr.properties 2 kB
- mixinUnitTest/.../messages_it.properties 2 kB
- mixinUnitTest/.../messages_ja.properties 4 kB
- mixinUnitTest/.../messages_nl.properties 3 kB
- mixinUnitTest/.../messages_pt_BR.properties 3 kB
- mixinUnitTest/.../messages_pt_PT.properties 3 kB
- mixinUnitTest/.../messages_ru.properties 4 kB
- mixinUnitTest/.../messages_sv.properties 3 kB
- mixinUnitTest/.../messages_th.properties 6 kB
- mixinUnitTest/.../messages_zh_CN.properties 2 kB
- mixinUnitTest/grails-app/views/error.gsp 0.3 kB
- mixinUnitTest/grails-app/views/index.gsp 3 kB
- mixinUnitTest/grails-app/.../main.gsp 2 kB
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
Almost a year later I tried the attached bug2 project against Grails 2.2.0 and I get zero test failures. It appears this was fixed somewhere between grails 2.0.0 and 2.2.0.
I believe it can be closed.
I tested out the mixinUnitTest project and it still fails. It is different from bug2 in that bug2 is using a static mixin on a POJO whereas mixinUnitTest is mixing in a regular Groovy object into a domain class.
This was fixed by the new grails.util.Mixin annotation which uses compile time mixins (Groovy's Mixin annotation uses metaprogramming)
I have the same issue here with a domain class that is using a @Mixin annotation. The first unit test that the class is used in has the methods available, but they are then removed in the cleanup phase. I've attached a sample project.