https://kotlinlang.org logo
#mockk
Title
# mockk
o

Ondřej Šimon

07/20/2022, 6:37 AM
Hello, I am trying to write a test in which I am mocking a call to a generic method and I am getting the following error:
Copy code
java.lang.InstantiationError: com.ColorRepository

	at jdk.internal.reflect.GeneratedSerializationConstructorAccessor7.newInstance(Unknown Source)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
	at io.mockk.proxy.jvm.ObjenesisInstantiator.instanceViaObjenesis(ObjenesisInstantiator.kt:75)
	at io.mockk.proxy.jvm.ObjenesisInstantiator.instance(ObjenesisInstantiator.kt:42)
	at io.mockk.impl.instantiation.JvmInstantiator$instantiate$2.invoke(JvmInstantiator.kt:16)
	at io.mockk.impl.instantiation.AbstractInstantiator.instantiateViaInstanceFactoryRegistry(AbstractInstantiator.kt:17)
	at io.mockk.impl.instantiation.JvmInstantiator.instantiate(JvmInstantiator.kt:15)
	at io.mockk.impl.recording.states.RecordingState$matcher$signatureValue$1$1.invoke(RecordingState.kt:49)
	at io.mockk.impl.instantiation.JvmAnyValueGenerator$anyValue$2.invoke(JvmAnyValueGenerator.kt:35)
	at io.mockk.impl.instantiation.AnyValueGenerator.anyValue(AnyValueGenerator.kt:34)
	at io.mockk.impl.instantiation.JvmAnyValueGenerator.anyValue(JvmAnyValueGenerator.kt:31)
	at io.mockk.impl.recording.states.RecordingState$matcher$signatureValue$1.invoke(RecordingState.kt:48)
	at io.mockk.impl.recording.JvmSignatureValueGenerator.signatureValue(JvmSignatureValueGenerator.kt:20)
	at io.mockk.impl.recording.states.RecordingState.matcher(RecordingState.kt:47)
	at io.mockk.impl.recording.CommonCallRecorder.matcher(CommonCallRecorder.kt:52)
	at com.ColorSynchronizationServiceTest$testSynchronizeFromErp - new sync manager instance is created and its method called$1.invoke(ColorSynchronizationServiceTest.kt:67)
	at com.ColorSynchronizationServiceTest$testSynchronizeFromErp - new sync manager instance is created and its method called$1.invoke(ColorSynchronizationServiceTest.kt:41)
	at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:25)
	at io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithRethrow$1.invoke(RecordedBlockEvaluator.kt:78)
	at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:40)
	at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalEvery(API.kt:93)
	at io.mockk.MockKKt.every(MockK.kt:98)
	at com.ColorSynchronizationServiceTest.testSynchronizeFromErp - new sync manager instance is created and its method called(ColorSynchronizationServiceTest.kt:41)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
The test looks like the following:
Copy code
@Test
fun `testSynchronizeFromErp - new sync manager instance is created and its method called`() {
	val improvedErpSynchronisationManagerMock =
		mockk<ImprovedErpSynchronisationManager<ColorErpDto, Color, ColorRepository>>()

	every {
		improvedErpSynchronizationManagerFactoryServiceMock
			.constructImprovedErpSynchronisationManager<ColorErpDto, Color, ColorRepository>(
				any(),
				any(),
				any(),
			)
	} returns improvedErpSynchronisationManagerMock

	every { improvedErpSynchronisationManagerMock.executeDataSynchronisationFromErp() } returns Unit

	colorSynchronizationService.synchronizeFromErp()

	verifySequence {
		improvedErpSynchronizationManagerFactoryServiceMock.constructImprovedErpSynchronisationManager<ColorErpDto, Color, ColorRepository>(
			any(),
			any(),
			any()
		)
	}
}
and the mocked generic method looks like the following:
Copy code
@Service
class ImprovedErpSynchronizationManagerFactoryService(
	private val platformTransactionManager: PlatformTransactionManager
) {
	fun <D : ErpDto, E : ErpEntity, R : ErpSynchronizableRepository<E>> constructImprovedErpSynchronisationManager(
		processedEntityType: Class<E>,
		repository: R,
		improvedErpDataLoaderAndTransformer: ImprovedErpDataLoaderAndTransformer<D, E>
	): ImprovedErpSynchronisationManager<D, E, R> {
		return ImprovedErpSynchronisationManager<D, E, R>(
			processedEntityType,
			repository,
			improvedErpDataLoaderAndTransformer,
			TransactionTemplate(platformTransactionManager)
		)
	}
}
I have been lost on this for a few days now and I have no idea how to fix this error. Do you have any hints?
🐛 1
🐞 1
137 Views