@@ -17,7 +17,6 @@ import org.utbot.framework.plugin.api.SpringCodeGenerationContext
1717import org.utbot.framework.plugin.api.SpringSettings.*
1818import org.utbot.framework.plugin.api.SpringConfiguration.*
1919import org.utbot.framework.plugin.api.util.IndentUtil.TAB
20- import org.utbot.framework.plugin.api.util.SpringModelUtils
2120import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId
2221import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
2322import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
@@ -26,8 +25,12 @@ import org.utbot.framework.plugin.api.util.SpringModelUtils.contextConfiguration
2625import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassId
2726import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId
2827import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassModeClassId
28+ import org.utbot.framework.plugin.api.util.SpringModelUtils.extendWithClassId
29+ import org.utbot.framework.plugin.api.util.SpringModelUtils.runWithClassId
30+ import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestClassId
2931import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestContextBootstrapperClassId
3032import org.utbot.framework.plugin.api.util.SpringModelUtils.springExtensionClassId
33+ import org.utbot.framework.plugin.api.util.SpringModelUtils.springRunnerClassId
3134import org.utbot.framework.plugin.api.util.SpringModelUtils.transactionalClassId
3235import org.utbot.framework.plugin.api.util.utContext
3336import org.utbot.spring.api.UTSpringContextLoadingException
@@ -99,23 +102,33 @@ class CgSpringIntegrationTestClassConstructor(
99102 )
100103
101104 private fun addNecessarySpringSpecificAnnotations () {
102- val springRunnerType = when (testFramework) {
103- Junit4 -> SpringModelUtils . runWithClassId
104- Junit5 -> SpringModelUtils . extendWithClassId
105+ val (testFrameworkExtension, springExtension) = when (testFramework) {
106+ Junit4 -> runWithClassId to springRunnerClassId
107+ Junit5 -> extendWithClassId to springExtensionClassId
105108 TestNg -> error(" Spring extension is not implemented in TestNg" )
106109 else -> error(" Trying to generate tests for Spring project with non-JVM framework" )
107110 }
108111
109112 addAnnotation(
110- classId = springRunnerType,
111- argument = createGetClassExpression(springExtensionClassId, codegenLanguage),
112- target = Class ,
113- )
114- addAnnotation(
115- classId = bootstrapWithClassId,
116- argument = createGetClassExpression(springBootTestContextBootstrapperClassId, codegenLanguage),
113+ classId = testFrameworkExtension,
114+ argument = createGetClassExpression(springExtension, codegenLanguage),
117115 target = Class ,
118116 )
117+
118+ if (utContext.classLoader.tryLoadClass(springBootTestContextBootstrapperClassId.name) != null )
119+ // TODO in somewhat new versions of Spring Boot, @SpringBootTest
120+ // already includes @BootstrapWith(SpringBootTestContextBootstrapper.class),
121+ // so we should avoid adding it manually to reduce number of annotations
122+ addAnnotation(
123+ classId = bootstrapWithClassId,
124+ argument = createGetClassExpression(springBootTestContextBootstrapperClassId, codegenLanguage),
125+ target = Class ,
126+ )
127+
128+ if (utContext.classLoader.tryLoadClass(springBootTestClassId.name) != null )
129+ addAnnotation(springBootTestClassId, Class )
130+
131+ // TODO avoid adding @ActiveProfiles(profiles = {"default"}) to reduce number of annotations
119132 addAnnotation(
120133 classId = activeProfilesClassId,
121134 namedArguments =
@@ -132,6 +145,8 @@ class CgSpringIntegrationTestClassConstructor(
132145 ),
133146 target = Class ,
134147 )
148+
149+ // TODO avoid adding @ContextConfiguration(classes = {$defaultBootConfigClass}) to reduce number of annotations
135150 addAnnotation(
136151 classId = contextConfigurationClassId,
137152 namedArguments =
0 commit comments