@@ -17,7 +17,6 @@ import org.utbot.framework.plugin.api.SpringCodeGenerationContext
17
17
import org.utbot.framework.plugin.api.SpringSettings.*
18
18
import org.utbot.framework.plugin.api.SpringConfiguration.*
19
19
import org.utbot.framework.plugin.api.util.IndentUtil.TAB
20
- import org.utbot.framework.plugin.api.util.SpringModelUtils
21
20
import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId
22
21
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
23
22
import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
@@ -26,8 +25,12 @@ import org.utbot.framework.plugin.api.util.SpringModelUtils.contextConfiguration
26
25
import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassId
27
26
import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId
28
27
import 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
29
31
import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestContextBootstrapperClassId
30
32
import org.utbot.framework.plugin.api.util.SpringModelUtils.springExtensionClassId
33
+ import org.utbot.framework.plugin.api.util.SpringModelUtils.springRunnerClassId
31
34
import org.utbot.framework.plugin.api.util.SpringModelUtils.transactionalClassId
32
35
import org.utbot.framework.plugin.api.util.utContext
33
36
import org.utbot.spring.api.UTSpringContextLoadingException
@@ -99,23 +102,33 @@ class CgSpringIntegrationTestClassConstructor(
99
102
)
100
103
101
104
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
105
108
TestNg -> error(" Spring extension is not implemented in TestNg" )
106
109
else -> error(" Trying to generate tests for Spring project with non-JVM framework" )
107
110
}
108
111
109
112
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),
117
115
target = Class ,
118
116
)
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
119
132
addAnnotation(
120
133
classId = activeProfilesClassId,
121
134
namedArguments =
@@ -132,6 +145,8 @@ class CgSpringIntegrationTestClassConstructor(
132
145
),
133
146
target = Class ,
134
147
)
148
+
149
+ // TODO avoid adding @ContextConfiguration(classes = {$defaultBootConfigClass}) to reduce number of annotations
135
150
addAnnotation(
136
151
classId = contextConfigurationClassId,
137
152
namedArguments =
0 commit comments