-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autotests for Spring non-injected fields (#2426)
- Loading branch information
Showing
17 changed files
with
219 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
2 changes: 1 addition & 1 deletion
2
...bot/examples/spring/autowiring/Order.java → ...g/autowiring/oneBeanForOneType/Order.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../examples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
...mples/spring/autowiring/OrderService.java → ...nForOneType/ServiceWithInjectedField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../src/main/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ain/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...in/java/org/utbot/examples/spring/autowiring/twoAndMoreBeansForOneType/PersonService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...amples/spring/autowiring/oneBeanForOneType/ServiceWithInjectedAndNonInjectedFieldTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} |
39 changes: 12 additions & 27 deletions
39
...es/spring/autowiring/OrderServiceTests.kt → ...rOneType/ServiceWithInjectedFieldTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?> | ||
} |
22 changes: 22 additions & 0 deletions
22
utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/utils/CallUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.