Skip to content

Commit

Permalink
Fix DummySpringBootIntegrationTestClass annotations (#2415)
Browse files Browse the repository at this point in the history
* Fix `DummySpringBootIntegrationTestClass` annotations

* Fix annotations in `CgSpringIntegrationTestClassConstructor`

* Replace `@RunWith(SpringExtension.class)` with `@RunWith(SpringRunner.class)`
  • Loading branch information
IlyaMuravjov authored Jul 16, 2023
1 parent 1116cbd commit b051634
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -132,6 +145,8 @@ class CgSpringIntegrationTestClassConstructor(
),
target = Class,
)

// TODO avoid adding @ContextConfiguration(classes = {$defaultBootConfigClass}) to reduce number of annotations
addAnnotation(
classId = contextConfigurationClassId,
namedArguments =
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b051634

Please sign in to comment.