From ab5897e5f2aa121a3d69d5832cb01598b833fa06 Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Fri, 14 Jul 2023 21:36:54 +0300 Subject: [PATCH 1/3] Fix `DummySpringBootIntegrationTestClass` annotations --- .../spring/dummy/DummySpringBootIntegrationTestClass.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utbot-spring-commons/src/main/kotlin/org/utbot/spring/dummy/DummySpringBootIntegrationTestClass.kt b/utbot-spring-commons/src/main/kotlin/org/utbot/spring/dummy/DummySpringBootIntegrationTestClass.kt index 54e017e462..0efc7128b0 100644 --- a/utbot-spring-commons/src/main/kotlin/org/utbot/spring/dummy/DummySpringBootIntegrationTestClass.kt +++ b/utbot-spring-commons/src/main/kotlin/org/utbot/spring/dummy/DummySpringBootIntegrationTestClass.kt @@ -1,11 +1,13 @@ package org.utbot.spring.dummy import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.SpringBootTestContextBootstrapper import org.springframework.test.context.BootstrapWith +@SpringBootTest @BootstrapWith(SpringBootTestContextBootstrapper::class) -class DummySpringBootIntegrationTestClass : DummySpringIntegrationTestClass() +open class DummySpringBootIntegrationTestClass : DummySpringIntegrationTestClass() @AutoConfigureTestDatabase -class DummySpringBootIntegrationTestClassAutoconfigTestDB : DummySpringIntegrationTestClass() +class DummySpringBootIntegrationTestClassAutoconfigTestDB : DummySpringBootIntegrationTestClass() From 0e4b6e11bf705cac3752f2395ede4a59c6b6c00e Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Fri, 14 Jul 2023 22:24:37 +0300 Subject: [PATCH 2/3] Fix annotations in `CgSpringIntegrationTestClassConstructor` --- .../plugin/api/util/SpringModelUtils.kt | 1 - ...CgSpringIntegrationTestClassConstructor.kt | 23 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt index b4a890d797..877ec34dd8 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt @@ -15,7 +15,6 @@ object SpringModelUtils { val applicationContextClassId = ClassId("org.springframework.context.ApplicationContext") val crudRepositoryClassId = ClassId("org.springframework.data.repository.CrudRepository") - @Suppress("unused", "may be used instead of ExtendWith + BootstrapWith in future") val springBootTestClassId = ClassId("org.springframework.boot.test.context.SpringBootTest") val dirtiesContextClassId = ClassId("org.springframework.test.annotation.DirtiesContext") diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt index a5d4299847..8dc56d2bd1 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt @@ -26,6 +26,7 @@ import org.utbot.framework.plugin.api.util.SpringModelUtils.contextConfiguration import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassModeClassId +import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestContextBootstrapperClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.springExtensionClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.transactionalClassId @@ -111,11 +112,21 @@ class CgSpringIntegrationTestClassConstructor( argument = createGetClassExpression(springExtensionClassId, codegenLanguage), target = Class, ) - addAnnotation( - classId = bootstrapWithClassId, - argument = createGetClassExpression(springBootTestContextBootstrapperClassId, codegenLanguage), - target = Class, - ) + + if (utContext.classLoader.tryLoadClass(springBootTestContextBootstrapperClassId.name) != null) + // TODO in somewhat new versions of Spring Boot, @SpringBootTest + // already includes @BootstrapWith(SpringBootTestContextBootstrapper.class), + // so we should avoid adding it manually to reduce number of annotations + addAnnotation( + classId = bootstrapWithClassId, + argument = createGetClassExpression(springBootTestContextBootstrapperClassId, codegenLanguage), + target = Class, + ) + + if (utContext.classLoader.tryLoadClass(springBootTestClassId.name) != null) + addAnnotation(springBootTestClassId, Class) + + // TODO avoid adding @ActiveProfiles(profiles = {"default"}) to reduce number of annotations addAnnotation( classId = activeProfilesClassId, namedArguments = @@ -132,6 +143,8 @@ class CgSpringIntegrationTestClassConstructor( ), target = Class, ) + + // TODO avoid adding @ContextConfiguration(classes = {$defaultBootConfigClass}) to reduce number of annotations addAnnotation( classId = contextConfigurationClassId, namedArguments = From 2ed04bc840af761d40ea1a574f198399d58a2b9d Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Fri, 14 Jul 2023 23:00:13 +0300 Subject: [PATCH 3/3] Replace `@RunWith(SpringExtension.class)` with `@RunWith(SpringRunner.class)` --- .../framework/plugin/api/util/SpringModelUtils.kt | 2 ++ .../CgSpringIntegrationTestClassConstructor.kt | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt index 877ec34dd8..5da925bc3b 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt @@ -23,6 +23,8 @@ object SpringModelUtils { val autoConfigureTestDbClassId = ClassId("org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase") val runWithClassId = ClassId("org.junit.runner.RunWith") + val springRunnerClassId = ClassId("org.springframework.test.context.junit4.SpringRunner") + val extendWithClassId = ClassId("org.junit.jupiter.api.extension.ExtendWith") val springExtensionClassId = ClassId("org.springframework.test.context.junit.jupiter.SpringExtension") diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt index 8dc56d2bd1..436a813f82 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt @@ -17,7 +17,6 @@ import org.utbot.framework.plugin.api.SpringCodeGenerationContext import org.utbot.framework.plugin.api.SpringSettings.* import org.utbot.framework.plugin.api.SpringConfiguration.* import org.utbot.framework.plugin.api.util.IndentUtil.TAB -import org.utbot.framework.plugin.api.util.SpringModelUtils import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId @@ -26,9 +25,12 @@ import org.utbot.framework.plugin.api.util.SpringModelUtils.contextConfiguration import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassModeClassId +import org.utbot.framework.plugin.api.util.SpringModelUtils.extendWithClassId +import org.utbot.framework.plugin.api.util.SpringModelUtils.runWithClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestContextBootstrapperClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.springExtensionClassId +import org.utbot.framework.plugin.api.util.SpringModelUtils.springRunnerClassId import org.utbot.framework.plugin.api.util.SpringModelUtils.transactionalClassId import org.utbot.framework.plugin.api.util.utContext import org.utbot.spring.api.UTSpringContextLoadingException @@ -100,16 +102,16 @@ class CgSpringIntegrationTestClassConstructor( ) private fun addNecessarySpringSpecificAnnotations() { - val springRunnerType = when (testFramework) { - Junit4 -> SpringModelUtils.runWithClassId - Junit5 -> SpringModelUtils.extendWithClassId + val (testFrameworkExtension, springExtension) = when (testFramework) { + Junit4 -> runWithClassId to springRunnerClassId + Junit5 -> extendWithClassId to springExtensionClassId TestNg -> error("Spring extension is not implemented in TestNg") else -> error("Trying to generate tests for Spring project with non-JVM framework") } addAnnotation( - classId = springRunnerType, - argument = createGetClassExpression(springExtensionClassId, codegenLanguage), + classId = testFrameworkExtension, + argument = createGetClassExpression(springExtension, codegenLanguage), target = Class, )