Using objenesis prevents running tests from interactive prompt more than once
Originally reported on Google Code with ID 306
What steps will reproduce the problem? If possible, provide source code and stack trace.
If I include the objenesis dependency in my BuildConfig, my tests run fine when executed
as 'grails test-app' but fail after the first run if I go into interactive mode by
typing 'grails' and then issuing 'test-app' calls. This has happened in two distinct
apps.
The stacktrace is here (I cannot get Grails to give me the entire stacktrace either
using -Dgrails.full.stacktrace=true or specifying grails.full.stacktrace = true in
Config.groovy):
org.spockframework.mock.CannotCreateMockException: Cannot create mock for class grails.plugins.springsecurity.SpringSecurityServicenull
at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:38)
at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:92)
at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47)
at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:282)
at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:99)
at com.xxxx.SystemServiceSpec.setup(SystemServiceSpec.groovy:17)
Caused by: java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.spockframework.mock.runtime.MockInstantiator$ObjenesisInstantiator.instantiate(MockInstantiator.java:45)
at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:31)
... 8 more
What version of Spock and Groovy are you using?
Spock 0.7, Grails 2.2.0 and 2.1.1
Reported by gregap on 2013-03-29 11:45:43
Issue 300 has been merged into this issue.
Reported by pniederw on 2013-04-10 06:16:45
I have the same issue but it is not linked to interactive mode.
It's happening when I have one unit spock test that mock a specific service and another
unit spock integration test that mock the same service.
If I run them together for being tested, it's not working. If I test them separatly,
it's working.
Reported by meyskens.laurent on 2013-04-15 09:35:25
I am not in the Grails environment, but I am having the same issue. Tests with mocked
resources pass in isolation but, when run as a testing suite, fail with the exception:
org.spockframework.mock.CannotCreateMockException: Cannot create mock for class [myClass]null
Is there a specific Mock teardown that needs to be performed to free up those resources
and allow future mock instances to be created?
I am seeing this with Spock 0.7, Groovy 1.8
Reported by mrmattrichards on 2013-05-02 19:19:44
Please provide a reproducible example.
Reported by pniederw on 2013-05-03 00:50:40
I did in the original report, which was marked as duplicate: https://code.google.com/p/spock/issues/detail?id=300
Reported by diaz.salvador on 2013-05-03 05:36:35
According to mrmattrichards, he is experiencing this problem outside Grails (or does
he just mean outside Grails interactive mode?). It would be very helpful if I could
reproduce this. If the problem only occurs in Grails (and this is my current understanding),
we will have to find a person knowledgable in Grails internals to look into this.
Reported by pniederw on 2013-05-03 06:09:15
I'm also experiencing this problem in a grails 2.3.0 app where we use mocks in unit
tests and also in integration tests.
Reported by dominicc1974 on 2013-10-24 12:52:43
When ran with jUnit, tests pass. This problem only occurs when running tests with grails.
Reported by alvar.lumberg on 2013-11-07 12:54:47
The bug doesn't occur when useObjenesis: false
Reported by alvar.lumberg on 2013-11-07 13:40:37
Having the same issue here when running both unit and integration grails tests and using
mocks in both type of tests with objenesis on the classpath. I can confirm that using
Mock(useObjenesis: false) when the mocking fails in integration tests is a valid workaround.
Reported by erdi84 on 2014-01-28 21:51:49
Issue 335 has been merged into this issue.
Reported by pniederw on 2014-01-28 21:55:52
Reported by pniederw on 2014-01-28 21:56:22
-
Status changed:
Accepted - Labels added: Milestone-1.0
See Issue 335. Recent Grails versions implement a workaround. Plan to fix for 1.0.
Reported by pniederw on 2014-01-28 21:58:32
Reported by pniederw on 2015-03-01 23:46:37
- Labels added: Milestone-1.1
- Labels removed: Milestone-1.0
I just found this issue by chance when searching for some other Objenesis-related information in the repository. Does this problem still occur or has it been fixed on purpose or due to some refactoring? Maybe someone can try with both Spock 1.3 and 2.0-M2.
I'm not sure we have an active Grails-capable person here. And Peter who at least had a plan is not on the project anymore. I guess a Grails-savvy user needs to test and / or provide a reproducer project with detailed instructions what to do for non-Grails people.