Spring Boot Integration Test started to fail after spring version upgrade

Viewed 14

After I upgraded Spring boot version 2.3.1.RELEASE to 2.6.1 for some reason my integration test started to fail due to OutOfMemory error.

Here is my test:

@Sql(
    scripts = [
        "classpath:sql/init_clear.sql",
        "classpath:sql/import/import_entities.sql"
    ],
    executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD
)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(
    initializers = [
        PostgreSQLContainerInitializer::class
    ]
)
@ActiveProfiles("dev")
class MyRepositoryTest {

    @Autowired
    private lateinit var repository: MyRepository

    @Test
    fun `Get all by id should work well`() {
        val id: Long = 1
        val entities = repository.getAllById(id)
        assertEquals(1, entities.size)
        assertEquals(id, entities[0].id)
    }

}

The stack trace of the error is the following:

org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 45.
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:63)
    at java.base@17.0.4/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base@17.0.4/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base@17.0.4/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base@17.0.4/java.lang.reflect.Method.invoke(Method.java:568)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at jdk.proxy2/jdk.proxy2.$Proxy5.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
    at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
    at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
Caused by: java.lang.OutOfMemoryError: Java heap space
    at ch.qos.logback.classic.spi.ThrowableProxyUtil.steArrayToStepArray(ThrowableProxyUtil.java:49)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:55)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60)

I debugged this issue a little more and found out that the error itself is produced due to work of the ResetMocksTestExecutionListener, to be more precise because it's beforeTestMethod function:

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    if (MOCKITO_IS_PRESENT) {
        resetMocks(testContext.getApplicationContext(), MockReset.BEFORE);
    }
}

And the real cause is java.lang.IllegalStateException: Failed to unwrap proxied object.

We have an option to stop using mockito in favor of mockk cause we use kotlin in this project but it's too expensive for us to do this in nearest future so it would be nice if there is some fast workarounds.

Thanks in advance for help!

0 Answers
Related