Skip to content

Commit 117f841

Browse files
Add specific annotations for Spring integration testing (#2281)
1 parent 8aedbbf commit 117f841

File tree

19 files changed

+411
-309
lines changed

19 files changed

+411
-309
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,28 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
1010
import org.utbot.framework.plugin.api.UtSpringContextModel
1111

1212
object SpringModelUtils {
13+
val autowiredClassId = ClassId("org.springframework.beans.factory.annotation.Autowired")
14+
1315
val applicationContextClassId = ClassId("org.springframework.context.ApplicationContext")
1416
val crudRepositoryClassId = ClassId("org.springframework.data.repository.CrudRepository")
1517

18+
@Suppress("unused", "may be used instead of ExtendWith + BootstrapWith in future")
19+
val springBootTestClassId = ClassId("org.springframework.boot.test.context.SpringBootTest")
20+
21+
val dirtiesContextClassId = ClassId("org.springframework.test.annotation.DirtiesContext")
22+
val dirtiesContextClassModeClassId = ClassId("org.springframework.test.annotation.DirtiesContext\$ClassMode")
23+
val transactionalClassId = ClassId("org.springframework.transaction.annotation.Transactional")
24+
val autoConfigureTestDbClassId = ClassId("org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase")
25+
26+
val runWithClassId = ClassId("org.junit.runner.RunWith")
27+
val extendWithClassId = ClassId("org.junit.jupiter.api.extension.ExtendWith")
28+
val springExtensionClassId = ClassId("org.springframework.test.context.junit.jupiter.SpringExtension")
29+
30+
val bootstrapWithClassId = ClassId("org.springframework.test.context.BootstrapWith")
31+
val springBootTestContextBootstrapperClassId =
32+
ClassId("org.springframework.boot.test.context.SpringBootTestContextBootstrapper")
33+
34+
1635
// most likely only one persistent library is on the classpath, but we need to be able to work with either of them
1736
private val persistentLibraries = listOf("javax.persistence", "jakarta.persistence")
1837
private fun persistentClassIds(simpleName: String) = persistentLibraries.map { ClassId("$it.$simpleName") }
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
package org.utbot.framework.codegen.domain.builtin
2-
3-
import org.utbot.framework.plugin.api.BuiltinClassId
4-
5-
internal val autowiredClassId = BuiltinClassId(
6-
canonicalName = "org.springframework.beans.factory.annotation.Autowired",
7-
simpleName = "Autowired",
8-
)

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import org.utbot.framework.codegen.domain.ParametrizedTestSource
77
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
88
import org.utbot.framework.codegen.domain.StaticsMocking
99
import org.utbot.framework.codegen.domain.TestFramework
10-
import org.utbot.framework.codegen.domain.models.CgAnnotation
11-
import org.utbot.framework.codegen.domain.models.CgExecutableCall
12-
import org.utbot.framework.codegen.domain.models.CgStatement
13-
import org.utbot.framework.codegen.domain.models.CgStatementExecutableCall
14-
import org.utbot.framework.codegen.domain.models.CgTestMethod
15-
import org.utbot.framework.codegen.domain.models.CgThisInstance
16-
import org.utbot.framework.codegen.domain.models.CgValue
17-
import org.utbot.framework.codegen.domain.models.CgVariable
1810
import kotlinx.collections.immutable.PersistentList
1911
import kotlinx.collections.immutable.PersistentMap
2012
import kotlinx.collections.immutable.PersistentSet
@@ -23,12 +15,10 @@ import kotlinx.collections.immutable.persistentMapOf
2315
import kotlinx.collections.immutable.persistentSetOf
2416
import org.utbot.framework.codegen.domain.UtModelWrapper
2517
import org.utbot.framework.codegen.domain.ProjectType
26-
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
2718
import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider
2819
import org.utbot.framework.codegen.domain.builtin.UtilClassFileMethodProvider
2920
import org.utbot.framework.codegen.domain.builtin.UtilMethodProvider
30-
import org.utbot.framework.codegen.domain.models.SimpleTestClassModel
31-
import org.utbot.framework.codegen.domain.models.CgParameterKind
21+
import org.utbot.framework.codegen.domain.models.*
3222
import org.utbot.framework.codegen.services.access.Block
3323
import org.utbot.framework.codegen.tree.EnvironmentFieldStateCache
3424
import org.utbot.framework.codegen.tree.importIfNeeded
@@ -304,11 +294,13 @@ interface CgContextOwner {
304294
}
305295
}
306296

307-
fun addAnnotation(annotation: CgAnnotation) {
308-
if (collectedMethodAnnotations.add(annotation)) {
309-
importIfNeeded(annotation.classId) // TODO: check how JUnit annotations are loaded
297+
fun createGetClassExpression(id: ClassId, codegenLanguage: CodegenLanguage): CgGetClass =
298+
when (codegenLanguage) {
299+
CodegenLanguage.JAVA -> CgGetJavaClass(id)
300+
CodegenLanguage.KOTLIN -> CgGetKotlinClass(id)
301+
}.also {
302+
importIfNeeded(id)
310303
}
311-
}
312304

313305
/**
314306
* This method sets up context for a new test class file generation and executes the given [block].

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,22 +357,46 @@ enum class CgTestMethodType(val displayName: String, val isThrowing: Boolean) {
357357

358358
// Annotations
359359

360+
enum class AnnotationTarget {
361+
Class,
362+
363+
Method,
364+
365+
Field,
366+
}
367+
360368
abstract class CgAnnotation : CgElement {
361369
abstract val classId: ClassId
370+
abstract val target: AnnotationTarget
362371
}
363372

373+
/**
374+
* NOTE: use `StatementConstructor.addAnnotation`
375+
* instead of explicit constructor call.
376+
*/
364377
class CgCommentedAnnotation(val annotation: CgAnnotation) : CgAnnotation() {
365378
override val classId: ClassId = annotation.classId
379+
override val target: AnnotationTarget = annotation.target
366380
}
367381

382+
/**
383+
* NOTE: use `StatementConstructor.addAnnotation`
384+
* instead of explicit constructor call.
385+
*/
368386
class CgSingleArgAnnotation(
369387
override val classId: ClassId,
370-
val argument: CgExpression
388+
val argument: CgExpression,
389+
override val target: AnnotationTarget,
371390
) : CgAnnotation()
372391

392+
/**
393+
* NOTE: use `StatementConstructor.addAnnotation`
394+
* instead of explicit constructor call.
395+
*/
373396
class CgMultipleArgsAnnotation(
374397
override val classId: ClassId,
375-
val arguments: MutableList<CgNamedAnnotationArgument>
398+
val arguments: MutableList<CgNamedAnnotationArgument>,
399+
override val target: AnnotationTarget,
376400
) : CgAnnotation()
377401

378402
data class CgArrayAnnotationArgument(
@@ -942,8 +966,16 @@ sealed class CgGetClass : CgReferenceExpression {
942966
override val type: ClassId = Class::class.id
943967
}
944968

969+
/**
970+
* NOTE: use `CgContext.createGetClassExpression`
971+
* instead of the explicit constructor call.
972+
*/
945973
data class CgGetJavaClass(override val classId: ClassId) : CgGetClass()
946974

975+
/**
976+
* NOTE: use `CgContext.createGetClassExpression`
977+
* instead of the explicit constructor call.
978+
*/
947979
data class CgGetKotlinClass(override val classId: ClassId) : CgGetClass()
948980

949981
// Executable calls

0 commit comments

Comments
 (0)