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..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 @@ -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") @@ -24,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 a5d4299847..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,8 +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 @@ -99,23 +102,33 @@ 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), - target = Class, - ) - addAnnotation( - classId = bootstrapWithClassId, - argument = createGetClassExpression(springBootTestContextBootstrapperClassId, codegenLanguage), + classId = testFrameworkExtension, + argument = createGetClassExpression(springExtension, 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 +145,8 @@ class CgSpringIntegrationTestClassConstructor( ), target = Class, ) + + // TODO avoid adding @ContextConfiguration(classes = {$defaultBootConfigClass}) to reduce number of annotations addAnnotation( classId = contextConfigurationClassId, namedArguments = 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()