Open OliverCulleyDeLange opened 6 years ago
Hi Olivier, I've had the same issue so I ended up creating this trait which can be imported in your specs. It is related to how objenesis is used to instanciate spock mocks. The workaround will clear the mock cache between each tests.
trait SpockObjenesisCacheTrait {
@Before
def clearClassCache() {
try {
final objStdField = ObjenesisHelper.getDeclaredField("OBJENESIS_STD")
objStdField.accessible = true
final objStd = objStdField.get(ObjenesisHelper)
final cacheField = ObjenesisBase.getDeclaredField("cache")
cacheField.accessible = true
cacheField.set(objStd, null)
} catch (Throwable e) {
// ignore, failed to patch
}
}
}
@npresseault, as this is a hack anyway, I replaced a lot of text with one line, my trait became:
trait ObjenesisCacheClearer {
@Before
def before() {
ObjenesisHelper.OBJENESIS_STD.cache = null
}
@After
def after() {
ObjenesisHelper.OBJENESIS_STD.cache = new ConcurrentHashMap<String, ObjectInstantiator<?>> ()
}
}
I'm using kotlin-testrunner to run Spock tests against a Kotlin Android project.
I'm seeing an error when i try to mock the same class in more than one Spec file. For example if i mock an Android View
View dummyView = Mock(View)
in two Spec files, i see the following error when the second spec file runs (The first spec file runs and passes):Initially I thought it was an issue with Spock, however when I don't use the SpotlinTestRunner, the issue goes away.
The issue isn't limited to Android classes, it happens when i try to mock my own Kotlin classes too.
You can see the error in my sample codebase, look at the
MainViewModelSpec
and run theUNIT Test
run configuration and you should see the issue.Thanks in advance!