From 4833af7a80a686d857a13544cf5b03c840ba556a Mon Sep 17 00:00:00 2001 From: Kirill Shishin <73890886+tepa46@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:26:49 +0300 Subject: [PATCH] Add autotests for Spring non-injected fields (#2426) --- .../CgAbstractSpringTestClassConstructor.kt | 1 + .../spring/autowiring/OrderRepository.java | 1 + .../{ => oneBeanForOneType}/Order.java | 2 +- ...erviceWithInjectedAndNonInjectedField.java | 22 +++++++ .../ServiceWithInjectedField.java} | 5 +- .../twoAndMoreBeansForOneType/Person.java | 22 +++++++ .../PersonConfig.java | 17 ++++++ .../PersonService.java | 17 ++++++ ...iceWithInjectedAndNonInjectedFieldTests.kt | 39 ++++++++++++ .../ServiceWithInjectedFieldTests.kt} | 39 ++++-------- .../utbot/examples/spring/utils/CallUtils.kt | 22 +++++++ .../utils/SpringTestingConfiguration.kt | 6 ++ .../org/utbot/testing/Configurations.kt | 13 +++- .../testing/TestCodeGeneratorPipeline.kt | 8 +-- .../testing/TestSpecificTestCaseGenerator.kt | 6 +- .../utbot/testing/UtValueTestCaseChecker.kt | 59 +++++++++++-------- .../main/kotlin/org/utbot/testing/Utils.kt | 5 +- 17 files changed, 219 insertions(+), 65 deletions(-) rename utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/{ => oneBeanForOneType}/Order.java (84%) create mode 100644 utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedField.java rename utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/{OrderService.java => oneBeanForOneType/ServiceWithInjectedField.java} (70%) create mode 100644 utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/Person.java create mode 100644 utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonConfig.java create mode 100644 utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonService.java create mode 100644 utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedFieldTests.kt rename utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/{OrderServiceTests.kt => oneBeanForOneType/ServiceWithInjectedFieldTests.kt} (52%) create mode 100644 utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/CallUtils.kt diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt index ad9992db19..e497d835ef 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt @@ -21,6 +21,7 @@ import org.utbot.framework.plugin.api.UtSpringContextModel import org.utbot.framework.plugin.api.util.SpringModelUtils.getBeanNameOrNull import org.utbot.framework.plugin.api.util.id import java.lang.Exception +import java.util.Collections.max abstract class CgAbstractSpringTestClassConstructor(context: CgContext) : CgAbstractTestClassConstructor(context) { diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java index a9c614a780..c4446c5924 100644 --- a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java @@ -1,6 +1,7 @@ package org.utbot.examples.spring.autowiring; import org.springframework.data.jpa.repository.JpaRepository; +import org.utbot.examples.spring.autowiring.oneBeanForOneType.Order; public interface OrderRepository extends JpaRepository { } diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/Order.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/Order.java similarity index 84% rename from utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/Order.java rename to utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/Order.java index da6c4e40dc..5a2f2529e1 100644 --- a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/Order.java +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/Order.java @@ -1,4 +1,4 @@ -package org.utbot.examples.spring.autowiring; +package org.utbot.examples.spring.autowiring.oneBeanForOneType; import lombok.*; import lombok.extern.jackson.Jacksonized; diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedField.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedField.java new file mode 100644 index 0000000000..89309ca35a --- /dev/null +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedField.java @@ -0,0 +1,22 @@ +package org.utbot.examples.spring.autowiring.oneBeanForOneType; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.utbot.examples.spring.autowiring.OrderRepository; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class ServiceWithInjectedAndNonInjectedField { + + public List selectedOrders = new ArrayList<>(); + + @Autowired + private OrderRepository orderRepository; + + public Integer getOrdersSize() { + return orderRepository.findAll().size() + selectedOrders.size(); + } + +} diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderService.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedField.java similarity index 70% rename from utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderService.java rename to utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedField.java index 5b8960d4aa..9b492c30d9 100644 --- a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderService.java +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedField.java @@ -1,12 +1,13 @@ -package org.utbot.examples.spring.autowiring; +package org.utbot.examples.spring.autowiring.oneBeanForOneType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.utbot.examples.spring.autowiring.OrderRepository; import java.util.List; @Service -public class OrderService { +public class ServiceWithInjectedField { @Autowired private OrderRepository orderRepository; diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/Person.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/Person.java new file mode 100644 index 0000000000..11522fcac4 --- /dev/null +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/Person.java @@ -0,0 +1,22 @@ +package org.utbot.examples.spring.autowiring.twoAndMoreBeansForOneType; + +public class Person { + private String firstName; + private String lastName; + + private Integer age; + + public Person(String firstName, String secondName, Integer age) { + this.firstName = firstName; + this.lastName = secondName; + this.age = age; + } + + public Integer getAge(){ + return age; + } + + public String name() { + return firstName + " " + lastName; + } +} \ No newline at end of file diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonConfig.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonConfig.java new file mode 100644 index 0000000000..78f2faf0c1 --- /dev/null +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonConfig.java @@ -0,0 +1,17 @@ +package org.utbot.examples.spring.autowiring.twoAndMoreBeansForOneType; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class PersonConfig { + @Bean + public Person personOne() { + return new Person("Eg", "or", 7); + } + + @Bean + public Person personTwo() { + return new Person("Kir", "ill", 6); + } +} diff --git a/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonService.java b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonService.java new file mode 100644 index 0000000000..91162dc0ce --- /dev/null +++ b/utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonService.java @@ -0,0 +1,17 @@ +package org.utbot.examples.spring.autowiring.twoAndMoreBeansForOneType; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + @Autowired + private Person personOne; + + @Autowired + private Person personTwo; + + public Integer ageSum(){ + return personOne.getAge() + personTwo.getAge(); + } +} \ No newline at end of file diff --git a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedFieldTests.kt b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedFieldTests.kt new file mode 100644 index 0000000000..266c999150 --- /dev/null +++ b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedFieldTests.kt @@ -0,0 +1,39 @@ +package org.utbot.examples.spring.autowiring.oneBeanForOneType + +import org.junit.jupiter.api.Test +import org.utbot.examples.spring.utils.findAllRepositoryCall +import org.utbot.examples.spring.utils.springAdditionalDependencies +import org.utbot.examples.spring.utils.springMockStrategy +import org.utbot.examples.spring.utils.standardSpringTestingConfigurations +import org.utbot.framework.plugin.api.MockStrategyApi +import org.utbot.testcheckers.eq +import org.utbot.testing.DoNotCalculate +import org.utbot.testing.UtValueTestCaseChecker +import org.utbot.testing.ignoreExecutionsNumber +import org.utbot.testing.isException +import org.utbot.testing.singleMock +import org.utbot.testing.value + +internal class ServiceWithInjectedAndNonInjectedFieldTests: UtValueTestCaseChecker( + testClass = ServiceWithInjectedAndNonInjectedField::class, + configurations = standardSpringTestingConfigurations +) { + @Test + fun testGetOrdersSize() { + checkThisMocksAndExceptions( + method = ServiceWithInjectedAndNonInjectedField::getOrdersSize, + // TODO: replace with `branches = eq(3)` + // after the fix of `speculativelyCannotProduceNullPointerException` in SpringApplicationContext + branches = ignoreExecutionsNumber, + { thisInstance, mocks, r: Result -> + val orderRepository = mocks.singleMock("orderRepository", findAllRepositoryCall) + val repositorySize = orderRepository.value?>()!!.size + repositorySize + thisInstance.selectedOrders.size == r.getOrNull() + }, + { _, _, r: Result -> r.isException() }, + coverage = DoNotCalculate, + mockStrategy = springMockStrategy, + additionalDependencies = springAdditionalDependencies, + ) + } +} \ No newline at end of file diff --git a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/OrderServiceTests.kt b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedFieldTests.kt similarity index 52% rename from utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/OrderServiceTests.kt rename to utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedFieldTests.kt index 36fe8e0689..20c59bb9e8 100644 --- a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/OrderServiceTests.kt +++ b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedFieldTests.kt @@ -1,62 +1,47 @@ -package org.utbot.examples.spring.autowiring +package org.utbot.examples.spring.autowiring.oneBeanForOneType import org.junit.jupiter.api.Test +import org.utbot.examples.spring.utils.findAllRepositoryCall +import org.utbot.examples.spring.utils.saveRepositoryCall import org.utbot.examples.spring.utils.springAdditionalDependencies +import org.utbot.examples.spring.utils.springMockStrategy import org.utbot.examples.spring.utils.standardSpringTestingConfigurations import org.utbot.framework.plugin.api.MockStrategyApi import org.utbot.testcheckers.eq import org.utbot.testing.* -import kotlin.reflect.full.functions -import kotlin.reflect.KFunction1 -import kotlin.reflect.KFunction2 -internal class OrderServiceTests : UtValueTestCaseChecker( - testClass = OrderService::class, +internal class ServiceWithInjectedFieldTests : UtValueTestCaseChecker( + testClass = ServiceWithInjectedField::class, configurations = standardSpringTestingConfigurations ) { @Test fun testGetOrders() { checkMocks( - method = OrderService::getOrders, + method = ServiceWithInjectedField::getOrders, branches = eq(1), { mocks, r -> val orderRepository = mocks.singleMock("orderRepository", findAllRepositoryCall) orderRepository.value?>() == r }, coverage = DoNotCalculate, - mockStrategy = MockStrategyApi.OTHER_CLASSES, + mockStrategy = springMockStrategy, additionalDependencies = springAdditionalDependencies, ) } @Test fun testCreateOrder() { - checkMocksWithExceptions( - method = OrderService::createOrder, + checkThisMocksAndExceptions( + method = ServiceWithInjectedField::createOrder, // TODO: replace with `branches = eq(1)` after fix of https://github.com/UnitTestBot/UTBotJava/issues/2367 branches = ignoreExecutionsNumber, - { _: Order?, mocks, r: Result -> + { _, _, mocks, r: Result -> val orderRepository = mocks.singleMock("orderRepository", saveRepositoryCall) orderRepository.value() == r.getOrNull() }, coverage = DoNotCalculate, - mockStrategy = MockStrategyApi.OTHER_CLASSES, + mockStrategy = springMockStrategy, additionalDependencies = springAdditionalDependencies, ) } - - @Suppress("UNCHECKED_CAST") - private val findAllRepositoryCall: KFunction1?> = - OrderRepository::class - .functions - .single { it.name == "findAll" && it.parameters.size == 1 } - as KFunction1?> - - - @Suppress("UNCHECKED_CAST") - private val saveRepositoryCall: KFunction2 = - OrderRepository::class - .functions - .single { it.name == "save" && it.parameters.size == 2 } - as KFunction2 } \ No newline at end of file diff --git a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/CallUtils.kt b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/CallUtils.kt new file mode 100644 index 0000000000..98ee4e02cf --- /dev/null +++ b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/CallUtils.kt @@ -0,0 +1,22 @@ +package org.utbot.examples.spring.utils + +import org.utbot.examples.spring.autowiring.OrderRepository +import org.utbot.examples.spring.autowiring.oneBeanForOneType.Order +import kotlin.reflect.KFunction1 +import kotlin.reflect.KFunction2 +import kotlin.reflect.full.functions + +@Suppress("UNCHECKED_CAST") +val findAllRepositoryCall: KFunction1?> = + OrderRepository::class + .functions + .single { it.name == "findAll" && it.parameters.size == 1 } + as KFunction1?> + + +@Suppress("UNCHECKED_CAST") +val saveRepositoryCall: KFunction2 = + OrderRepository::class + .functions + .single { it.name == "save" && it.parameters.size == 2 } + as KFunction2 \ No newline at end of file diff --git a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/SpringTestingConfiguration.kt b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/SpringTestingConfiguration.kt index 63f83be1c2..d311a46dde 100644 --- a/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/SpringTestingConfiguration.kt +++ b/utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/SpringTestingConfiguration.kt @@ -4,6 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.repository.PagingAndSortingRepository import org.utbot.framework.codegen.domain.ParametrizedTestSource import org.utbot.framework.plugin.api.CodegenLanguage +import org.utbot.framework.plugin.api.MockStrategyApi +import org.utbot.framework.plugin.api.SpringApplicationContext +import org.utbot.framework.plugin.api.SpringSettings +import org.utbot.framework.plugin.api.SpringTestType import org.utbot.testing.SpringConfiguration import org.utbot.testing.TestExecution @@ -11,6 +15,8 @@ val standardSpringTestingConfigurations: List = listOf( SpringConfiguration(CodegenLanguage.JAVA, ParametrizedTestSource.DO_NOT_PARAMETRIZE, TestExecution) ) +val springMockStrategy = MockStrategyApi.OTHER_CLASSES + val springAdditionalDependencies: Array> = arrayOf( JpaRepository::class.java, PagingAndSortingRepository::class.java, diff --git a/utbot-testing/src/main/kotlin/org/utbot/testing/Configurations.kt b/utbot-testing/src/main/kotlin/org/utbot/testing/Configurations.kt index 89f89f93d4..8fe673c418 100644 --- a/utbot-testing/src/main/kotlin/org/utbot/testing/Configurations.kt +++ b/utbot-testing/src/main/kotlin/org/utbot/testing/Configurations.kt @@ -3,6 +3,9 @@ package org.utbot.testing import org.utbot.framework.codegen.domain.ParametrizedTestSource import org.utbot.framework.codegen.domain.ProjectType import org.utbot.framework.plugin.api.CodegenLanguage +import org.utbot.framework.plugin.api.SpringApplicationContext +import org.utbot.framework.plugin.api.SpringSettings +import org.utbot.framework.plugin.api.SpringTestType abstract class AbstractConfiguration( val projectType: ProjectType, @@ -21,4 +24,12 @@ data class SpringConfiguration( override val language: CodegenLanguage, override val parametrizedTestSource: ParametrizedTestSource, override val lastStage: Stage, -): AbstractConfiguration(ProjectType.Spring, language, parametrizedTestSource, lastStage) \ No newline at end of file +): AbstractConfiguration(ProjectType.Spring, language, parametrizedTestSource, lastStage) + +val defaultSpringApplicationContext = SpringApplicationContext( + mockInstalled = true, + staticsMockingIsConfigured = true, + shouldUseImplementors = false, + springTestType = SpringTestType.UNIT_TEST, + springSettings = SpringSettings.AbsentSpringSettings(), +) \ No newline at end of file diff --git a/utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt b/utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt index c48c7f6f24..1e9c66f417 100644 --- a/utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt +++ b/utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt @@ -268,13 +268,7 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes parameterizedTestSource = parametrizedTestSource, runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour, enableTestsTimeout = enableTestsTimeout, - springCodeGenerationContext = SpringApplicationContext( - mockInstalled = true, - staticsMockingIsConfigured = true, - shouldUseImplementors = false, - springTestType = SpringTestType.UNIT_TEST, - springSettings = SpringSettings.AbsentSpringSettings(), - ) + springCodeGenerationContext = defaultSpringApplicationContext, ) ProjectType.PureJvm -> CodeGenerator( classUnderTest.id, diff --git a/utbot-testing/src/main/kotlin/org/utbot/testing/TestSpecificTestCaseGenerator.kt b/utbot-testing/src/main/kotlin/org/utbot/testing/TestSpecificTestCaseGenerator.kt index d3c8a293b5..1cadbad0b1 100644 --- a/utbot-testing/src/main/kotlin/org/utbot/testing/TestSpecificTestCaseGenerator.kt +++ b/utbot-testing/src/main/kotlin/org/utbot/testing/TestSpecificTestCaseGenerator.kt @@ -9,9 +9,11 @@ import org.utbot.engine.UtBotSymbolicEngine import org.utbot.engine.util.mockListeners.ForceMockListener import org.utbot.engine.util.mockListeners.ForceStaticMockListener import org.utbot.framework.UtSettings +import org.utbot.framework.plugin.api.ApplicationContext import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.ExecutableId import org.utbot.framework.plugin.api.MockStrategyApi +import org.utbot.framework.plugin.api.SpringApplicationContext import org.utbot.framework.plugin.api.TestCaseGenerator import org.utbot.framework.plugin.api.UtError import org.utbot.framework.plugin.api.UtExecution @@ -35,6 +37,7 @@ class TestSpecificTestCaseGenerator( engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(), isCanceled: () -> Boolean = { false }, private val taintConfigurationProvider: TaintConfigurationProvider? = null, + applicationContext: ApplicationContext = ApplicationContext(), ): TestCaseGenerator( listOf(buildDir), classpath, @@ -42,7 +45,8 @@ class TestSpecificTestCaseGenerator( JdkInfoDefaultProvider().info, engineActions, isCanceled, - forceSootReload = false + forceSootReload = false, + applicationContext = applicationContext, ) { private val logger = KotlinLogging.logger {} diff --git a/utbot-testing/src/main/kotlin/org/utbot/testing/UtValueTestCaseChecker.kt b/utbot-testing/src/main/kotlin/org/utbot/testing/UtValueTestCaseChecker.kt index 37ad0f65ec..8a25900d8c 100644 --- a/utbot-testing/src/main/kotlin/org/utbot/testing/UtValueTestCaseChecker.kt +++ b/utbot-testing/src/main/kotlin/org/utbot/testing/UtValueTestCaseChecker.kt @@ -15,6 +15,7 @@ import org.utbot.framework.coverage.Coverage import org.utbot.framework.coverage.counters import org.utbot.framework.coverage.methodCoverage import org.utbot.framework.coverage.toAtLeast +import org.utbot.framework.plugin.api.ApplicationContext import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.ExecutableId import org.utbot.framework.plugin.api.FieldId @@ -25,6 +26,9 @@ import org.utbot.framework.plugin.api.MockStrategyApi import org.utbot.framework.plugin.api.MockStrategyApi.NO_MOCKS import org.utbot.framework.plugin.api.ObjectMockTarget import org.utbot.framework.plugin.api.ParameterMockTarget +import org.utbot.framework.plugin.api.SpringApplicationContext +import org.utbot.framework.plugin.api.SpringSettings +import org.utbot.framework.plugin.api.SpringTestType import org.utbot.framework.plugin.api.UtCompositeModel import org.utbot.framework.plugin.api.UtConcreteValue import org.utbot.framework.plugin.api.UtInstrumentation @@ -61,7 +65,7 @@ import kotlin.reflect.KFunction5 abstract class UtValueTestCaseChecker( testClass: KClass<*>, testCodeGeneration: Boolean = true, - configurations: List = standardTestingConfigurations, + val configurations: List = standardTestingConfigurations, ) : CodeGenerationIntegrationTest(testClass, testCodeGeneration, configurations) { // contains already analyzed by the engine methods private val analyzedMethods: MutableMap = mutableMapOf() @@ -530,77 +534,77 @@ abstract class UtValueTestCaseChecker( additionalMockAlwaysClasses = additionalMockAlwaysClasses ) - protected inline fun checkMocksWithExceptions( - method: KFunction1<*, R>, + protected inline fun checkThisMocksAndExceptions( + method: KFunction1, branches: ExecutionsNumberMatcher, - vararg matchers: (Mocks, Result) -> Boolean, + vararg matchers: (T, Mocks, Result) -> Boolean, coverage: CoverageMatcher = Full, mockStrategy: MockStrategyApi = NO_MOCKS, additionalDependencies: Array> = emptyArray(), additionalMockAlwaysClasses: Set = emptySet() ) = internalCheck( method, mockStrategy, branches, matchers, coverage, - arguments = ::withMocksAndExceptions, + arguments = ::withThisMocksAndExceptions, additionalDependencies = additionalDependencies, additionalMockAlwaysClasses = additionalMockAlwaysClasses ) - protected inline fun checkMocksWithExceptions( - method: KFunction2<*, T, R>, + protected inline fun checkThisMocksAndExceptions( + method: KFunction2, branches: ExecutionsNumberMatcher, - vararg matchers: (T, Mocks, Result) -> Boolean, + vararg matchers: (T, T1, Mocks, Result) -> Boolean, coverage: CoverageMatcher = Full, mockStrategy: MockStrategyApi = NO_MOCKS, additionalDependencies: Array> = emptyArray(), additionalMockAlwaysClasses: Set = emptySet() ) = internalCheck( - method, mockStrategy, branches, matchers, coverage, T::class, - arguments = ::withMocksAndExceptions, + method, mockStrategy, branches, matchers, coverage, T1::class, + arguments = ::withThisMocksAndExceptions, additionalDependencies = additionalDependencies, additionalMockAlwaysClasses = additionalMockAlwaysClasses ) - protected inline fun checkMocksWithExceptions( - method: KFunction3<*, T1, T2, R>, + protected inline fun checkThisMocksAndExceptions( + method: KFunction3, branches: ExecutionsNumberMatcher, - vararg matchers: (T1, T2, Mocks, Result) -> Boolean, + vararg matchers: (T, T1, T2, Mocks, Result) -> Boolean, coverage: CoverageMatcher = Full, mockStrategy: MockStrategyApi = NO_MOCKS, additionalDependencies: Array> = emptyArray(), additionalMockAlwaysClasses: Set = emptySet() ) = internalCheck( method, mockStrategy, branches, matchers, coverage, T1::class, T2::class, - arguments = ::withMocksAndExceptions, + arguments = ::withThisMocksAndExceptions, additionalDependencies = additionalDependencies, additionalMockAlwaysClasses = additionalMockAlwaysClasses ) - protected inline fun checkMocksWithExceptions( - method: KFunction4<*, T1, T2, T3, R>, + protected inline fun checkThisMocksAndExceptions( + method: KFunction4, branches: ExecutionsNumberMatcher, - vararg matchers: (T1, T2, T3, Mocks, Result) -> Boolean, + vararg matchers: (T, T1, T2, T3, Mocks, Result) -> Boolean, coverage: CoverageMatcher = Full, mockStrategy: MockStrategyApi = NO_MOCKS, additionalDependencies: Array> = emptyArray(), additionalMockAlwaysClasses: Set = emptySet() ) = internalCheck( method, mockStrategy, branches, matchers, coverage, T1::class, T2::class, T3::class, - arguments = ::withMocksAndExceptions, + arguments = ::withThisMocksAndExceptions, additionalDependencies = additionalDependencies, additionalMockAlwaysClasses = additionalMockAlwaysClasses ) - protected inline fun checkMocksWithExceptions( - method: KFunction5<*, T1, T2, T3, T4, R>, + protected inline fun checkThisMocksAndExceptions( + method: KFunction5, branches: ExecutionsNumberMatcher, - vararg matchers: (T1, T2, T3, T4, Mocks, Result) -> Boolean, + vararg matchers: (T, T1, T2, T3, T4, Mocks, Result) -> Boolean, coverage: CoverageMatcher = Full, mockStrategy: MockStrategyApi = NO_MOCKS, additionalDependencies: Array> = emptyArray(), additionalMockAlwaysClasses: Set = emptySet() ) = internalCheck( method, mockStrategy, branches, matchers, coverage, T1::class, T2::class, T3::class, T4::class, - arguments = ::withMocksAndExceptions, + arguments = ::withThisMocksAndExceptions, additionalDependencies = additionalDependencies, additionalMockAlwaysClasses = additionalMockAlwaysClasses ) @@ -2158,7 +2162,12 @@ abstract class UtValueTestCaseChecker( TestSpecificTestCaseGenerator( buildInfo.buildDir, buildInfo.dependencyPath, - System.getProperty("java.class.path") + System.getProperty("java.class.path"), + applicationContext = if (configurations.any { it is SpringConfiguration }) { + defaultSpringApplicationContext + } else { + ApplicationContext() + } ) } @@ -2307,8 +2316,8 @@ fun withMocks(ex: UtValueExecution<*>) = ex.paramsBefore + listOf(ex.mocks) + ex fun withMocksAndInstrumentation(ex: UtValueExecution<*>) = ex.paramsBefore + listOf(ex.mocks) + listOf(ex.instrumentation) + ex.evaluatedResult -fun withMocksAndExceptions(ex: UtValueExecution<*>) = - ex.paramsBefore + listOf(ex.mocks) + ex.returnValue +fun withThisMocksAndExceptions(ex: UtValueExecution<*>) = + listOf(ex.callerBefore) + ex.paramsBefore + listOf(ex.mocks) + ex.returnValue fun withMocksInstrumentationAndThis(ex: UtValueExecution<*>) = listOf(ex.callerBefore) + ex.paramsBefore + listOf(ex.mocks) + listOf(ex.instrumentation) + ex.evaluatedResult diff --git a/utbot-testing/src/main/kotlin/org/utbot/testing/Utils.kt b/utbot-testing/src/main/kotlin/org/utbot/testing/Utils.kt index 6c927d85b6..a912eb9791 100644 --- a/utbot-testing/src/main/kotlin/org/utbot/testing/Utils.kt +++ b/utbot-testing/src/main/kotlin/org/utbot/testing/Utils.kt @@ -1,5 +1,8 @@ package org.utbot.testing +import org.utbot.framework.plugin.api.SpringApplicationContext +import org.utbot.framework.plugin.api.SpringSettings +import org.utbot.framework.plugin.api.SpringTestType import org.utbot.framework.plugin.api.UtExecutionFailure import org.utbot.framework.plugin.api.UtExecutionResult import org.utbot.framework.plugin.api.UtExecutionSuccess @@ -8,4 +11,4 @@ import org.utbot.framework.plugin.api.UtModel fun UtExecutionResult.getOrThrow(): UtModel = when (this) { is UtExecutionSuccess -> model is UtExecutionFailure -> throw exception -} \ No newline at end of file +}