Skip to content

Commit

Permalink
Fix test with two models of the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
tepa46 committed Jul 25, 2023
1 parent 89a8400 commit a99a6d0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ class CgMockedFieldsManager(context: CgContext) : CgClassFieldManagerImpl(contex
override fun fieldWithAnnotationIsRequired(modelWrappers: Set<UtModelWrapper>): Boolean {
// group [listOfUtModels] by `testSetId` and `executionId`
// to check how many instances of one type are used in each execution
val modelsByExecutions = modelWrappers
val modelWrappersByExecutions = modelWrappers
.groupByTo(HashMap()) { Pair(it.testSetId, it.executionId) }

// maximal instances of one type amount in one execution
val instanceMaxCount = max(modelsByExecutions.map { (_, modelsList) -> modelsList.size })
// maximal count of instances of the same type amount in one execution
// we use `modelTagName` in order to distinguish mock models by their name
val maxCountOfInstancesOfTheSameTypeByExecution = max(modelWrappersByExecutions.map { (_, modelsList) -> modelsList.size })

// if [instanceCount] is 1, then we mock variable by @Mock annotation
// if [maxCountOfInstancesOfTheSameTypeByExecution] is 1, then we mock variable by @Mock annotation
// Otherwise we will mock variable by simple mock later
return instanceMaxCount == 1
return maxCountOfInstancesOfTheSameTypeByExecution == 1
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.utbot.examples.spring.autowiring.twoAndMoreBeansForOneType;

public class Person {
private Integer age;
private String firstName;
private String lastName;

private Integer weight;
private Integer age;

public Person(Integer age, Integer weight) {
public Person(String firstName, String secondName, Integer age) {
this.firstName = firstName;
this.lastName = secondName;
this.age = age;
this.weight = weight;
}

public Integer getAge(){
return age;
public String getName() {
return firstName + " " + lastName;
}

public Integer getWeight() {
return weight;
public Integer getAge(){
return age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Integer ageSum(){
}

// a method for testing the case when the Engine produces two models on @Autowired variables of the same type
public Integer joinInfo(){
return personOne.getWeight() + personTwo.getAge() + baseOrders.size();
public Boolean checker() {
return personOne.getName().equals("k") && personTwo.getName().length() > 5 && baseOrders.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,86 @@ class ServiceOfBeansWithSameTypeTest : SpringNoConfigUtValueTestCaseChecker(
) {

/**
* In this test, we check the case when the Engine reproduces two models on two @Autowired variables of the same type.
* In this test, we check the case when the Engine produces two models on two @Autowired variables of the same type.
*
* Therefore, we mock all the variables and get the only necessary `mock.values` in each variable
* The engine produce two models only in the tests, when `baseOrder` is a testing participant.
* In these tests, we mock all the variables and get the only necessary `mock.values` in each variable.
*/
@Test
fun testJoin() {
fun testChecker() {
checkThisMocksAndExceptions(
method = ServiceOfBeansWithSameType::joinInfo,
branches = eq(3),
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)
method = ServiceOfBeansWithSameType::checker,
branches = eq(6),
{_, mocks, r ->
val personOne = mocks.singleMock("personOne", namePersonCall)

val personOneWeight = personOne.value<Int?>()
val personOneName = personOne.value<String?>()

val isPersonOneWeightNull = personOneWeight == null
val r1 = personOneName == null

isPersonOneWeightNull && r.isException<NullPointerException>()
r1 && r.isException<NullPointerException>()
},
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)
val personTwo = mocks.singleMock("personTwo", agePersonCall)
{_, mocks, r ->
val person = mocks.singleMock("personOne", namePersonCall)

val personOneName = (person.values[0] as? UtConcreteValue<*>)?.value
val personTwoName = (person.values[1] as? UtConcreteValue<*>)?.value

val r1 = personOneName == "k"
val r2 = personTwoName == null

val personOneWeight = personOne.value<Int?>()
val personTwoAge = personTwo.value<Int?>()
r1 && r2 && r.isException<NullPointerException>()
},
{_, mocks, r ->
val personOne = mocks.singleMock("personOne", namePersonCall)

val personOneName = personOne.value<String?>()

val isPersonOneWeightNotNull = personOneWeight != null
val isPersonTwoAgeNull = personTwoAge == null
val r1 = personOneName != "k"

isPersonOneWeightNotNull && isPersonTwoAgeNull && r.isException<NullPointerException>()
r1 && (r.getOrNull() == false)
},
{ thisInstance, mocks, r ->
val personOne = mocks.singleMock("personOne", weightPersonCall)
val personTwo = mocks.singleMock("personTwo", agePersonCall)
{_, mocks, r ->
val person = mocks.singleMock("personOne", namePersonCall)

val personOneName = (person.values[0] as? UtConcreteValue<*>)?.value
val personTwoName = (person.values[1] as? UtConcreteValue<*>)?.value.toString()

val personOneWeight = personOne.value<Int?>()!!
val personTwoAge = personTwo.value<Int?>()!!
val baseOrders = thisInstance.baseOrders!!
val r1 = personOneName == "k"
val r2 = personTwoName.length <= 5

personOneWeight + personTwoAge + baseOrders.size == r.getOrNull()
r1 && r2 && (r.getOrNull() == false)
},

//In this test Engine produces two models on two @Autowired variables of the same type
{thisInstance, mocks, r ->
val personOne = mocks.singleMock("personOne", namePersonCall)
val personTwo = mocks.singleMock("personTwo", namePersonCall)

val personOneName = (personOne.values[0] as? UtConcreteValue<*>)?.value
val personTwoName = (personTwo.values[0] as? UtConcreteValue<*>)?.value.toString()
val baseOrders = thisInstance.baseOrders

val r1 = personOneName == "k"
val r2 = personTwoName.length > 5
val r3 = baseOrders.isEmpty()

r1 && r2 && r3 && (r.getOrNull() == true)
},

{thisInstance, mocks, r ->
val personOne = mocks.singleMock("personOne", namePersonCall)
val personTwo = mocks.singleMock("personTwo", namePersonCall)

val personOneName = (personOne.values[0] as? UtConcreteValue<*>)?.value
val personTwoName = (personTwo.values[0] as? UtConcreteValue<*>)?.value.toString()
val baseOrders = thisInstance.baseOrders

val r1 = personOneName == "k"
val r2 = personTwoName.length > 5
val r3 = baseOrders.isNotEmpty()

r1 && r2 && r3 && (r.getOrNull() == false)
},
coverage = DoNotCalculate,
mockStrategy = springMockStrategy,
Expand All @@ -59,15 +100,15 @@ class ServiceOfBeansWithSameTypeTest : SpringNoConfigUtValueTestCaseChecker(
}

/**
* In this test, we check the case when the Engine reproduces one model on two @Autowired variables of the same type.
* In this test, we check the case when the Engine produces one model on two @Autowired variables of the same type.
*
* Therefore, we only mock one of the variables and get all `mock.values` in it
*/
@Test
fun testAgeSum(){
checkThisMocksAndExceptions(
method = ServiceOfBeansWithSameType::ageSum,
branches = ignoreExecutionsNumber,
branches = eq(3),
{ _, mocks, r ->
val personOne = mocks.singleMock("personOne", agePersonCall)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ val saveRepositoryCall: KFunction2<OrderRepository, Order?, Order?> =


@Suppress("UNCHECKED_CAST")
val weightPersonCall: KFunction1<Person, Int?> =
val namePersonCall: KFunction1<Person, String?> =
Person::class
.functions
.single { it.name == "getWeight" && it.parameters.size == 1 }
as KFunction1<Person, Int?>
.single { it.name == "getName" && it.parameters.size == 1 }
as KFunction1<Person, String?>

@Suppress("UNCHECKED_CAST")
val agePersonCall: KFunction1<Person, Int?> =
Expand Down

0 comments on commit a99a6d0

Please sign in to comment.