Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autotests for Spring non-injected fields + bug fix #2426

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpringTestClassModel>(context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Order, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.utbot.examples.spring.autowiring;
package org.utbot.examples.spring.autowiring.oneBeanForOneType;

import lombok.*;
import lombok.extern.jackson.Jacksonized;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Order> selectedOrders = new ArrayList<>();

@Autowired
private OrderRepository orderRepository;

public Integer getOrdersSize() {
return orderRepository.findAll().size() + selectedOrders.size();
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Int> ->
val orderRepository = mocks.singleMock("orderRepository", findAllRepositoryCall)
val repositorySize = orderRepository.value<List<Order>?>()!!.size
repositorySize + thisInstance.selectedOrders.size == r.getOrNull()
},
{ _, _, r: Result<Int> -> r.isException<NullPointerException>() },
coverage = DoNotCalculate,
mockStrategy = springMockStrategy,
additionalDependencies = springAdditionalDependencies,
)
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Order>?>() == 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<Order?> ->
{ _, _, mocks, r: Result<Order?> ->
val orderRepository = mocks.singleMock("orderRepository", saveRepositoryCall)
orderRepository.value<Order?>() == r.getOrNull()
},
coverage = DoNotCalculate,
mockStrategy = MockStrategyApi.OTHER_CLASSES,
mockStrategy = springMockStrategy,
additionalDependencies = springAdditionalDependencies,
)
}

@Suppress("UNCHECKED_CAST")
private val findAllRepositoryCall: KFunction1<OrderRepository, List<Order>?> =
OrderRepository::class
.functions
.single { it.name == "findAll" && it.parameters.size == 1 }
as KFunction1<OrderRepository, List<Order>?>


@Suppress("UNCHECKED_CAST")
private val saveRepositoryCall: KFunction2<OrderRepository, Order?, Order?> =
OrderRepository::class
.functions
.single { it.name == "save" && it.parameters.size == 2 }
as KFunction2<OrderRepository, Order?, Order?>
}
Original file line number Diff line number Diff line change
@@ -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, List<Order>?> =
OrderRepository::class
.functions
.single { it.name == "findAll" && it.parameters.size == 1 }
as KFunction1<OrderRepository, List<Order>?>


@Suppress("UNCHECKED_CAST")
val saveRepositoryCall: KFunction2<OrderRepository, Order?, Order?> =
OrderRepository::class
.functions
.single { it.name == "save" && it.parameters.size == 2 }
as KFunction2<OrderRepository, Order?, Order?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ 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

val standardSpringTestingConfigurations: List<SpringConfiguration> = listOf(
SpringConfiguration(CodegenLanguage.JAVA, ParametrizedTestSource.DO_NOT_PARAMETRIZE, TestExecution)
)

val springMockStrategy = MockStrategyApi.OTHER_CLASSES

val springAdditionalDependencies: Array<Class<*>> = arrayOf(
JpaRepository::class.java,
PagingAndSortingRepository::class.java,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
): AbstractConfiguration(ProjectType.Spring, language, parametrizedTestSource, lastStage)

val defaultSpringApplicationContext = SpringApplicationContext(
mockInstalled = true,
staticsMockingIsConfigured = true,
shouldUseImplementors = false,
springTestType = SpringTestType.UNIT_TEST,
springSettings = SpringSettings.AbsentSpringSettings(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,14 +37,16 @@ class TestSpecificTestCaseGenerator(
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
isCanceled: () -> Boolean = { false },
private val taintConfigurationProvider: TaintConfigurationProvider? = null,
applicationContext: ApplicationContext = ApplicationContext(),
): TestCaseGenerator(
listOf(buildDir),
classpath,
dependencyPaths,
JdkInfoDefaultProvider().info,
engineActions,
isCanceled,
forceSootReload = false
forceSootReload = false,
applicationContext = applicationContext,
) {

private val logger = KotlinLogging.logger {}
Expand Down
Loading
Loading