-
Notifications
You must be signed in to change notification settings - Fork 45
First autotests for tests generation on Spring projects #2365
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| plugins { | ||
| id 'java-library' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(project(":utbot-api")) | ||
|
|
||
| implementation 'org.projectlombok:lombok:1.18.20' | ||
| annotationProcessor 'org.projectlombok:lombok:1.18.20' | ||
|
|
||
| implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion | ||
| implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion | ||
| } | ||
|
|
||
| // This is required to avoid conflict between SpringBoot standard logger and the logger of our project. | ||
| // See https://stackoverflow.com/a/28735604 for more details. | ||
| configurations { | ||
| all { | ||
| exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/Order.java
This file contains hidden or 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,24 @@ | ||
| package org.utbot.examples.spring.autowiring; | ||
|
|
||
| import lombok.*; | ||
| import lombok.extern.jackson.Jacksonized; | ||
|
|
||
| import javax.persistence.*; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| @ToString | ||
| @Jacksonized | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Entity | ||
| @Table(name = "orders") | ||
| public class Order { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| Long id; | ||
| String buyer; | ||
| Double price; | ||
| int qty; | ||
| } |
6 changes: 6 additions & 0 deletions
6
utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderRepository.java
This file contains hidden or 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,6 @@ | ||
| package org.utbot.examples.spring.autowiring; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface OrderRepository extends JpaRepository<Order, Long> { | ||
| } |
21 changes: 21 additions & 0 deletions
21
utbot-spring-sample/src/main/java/org/utbot/examples/spring/autowiring/OrderService.java
This file contains hidden or 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,21 @@ | ||
| package org.utbot.examples.spring.autowiring; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class OrderService { | ||
|
|
||
| @Autowired | ||
| private OrderRepository orderRepository; | ||
|
|
||
| public List<Order> getOrders() { | ||
| return orderRepository.findAll(); | ||
| } | ||
|
|
||
| public Order createOrder(Order order) { | ||
| return orderRepository.save(order); | ||
| } | ||
| } |
This file contains hidden or 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,31 @@ | ||
| dependencies { | ||
| testImplementation(project(":utbot-framework")) | ||
| testImplementation project(':utbot-testing') | ||
| testImplementation project(':utbot-spring-sample') | ||
|
|
||
| // To use JUnit4, comment out JUnit5 and uncomment JUnit4 dependencies here. Please also check "test" section | ||
| // testImplementation group: 'junit', name: 'junit', version: '4.13.1' | ||
| testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.8.1' | ||
| testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.1' | ||
|
|
||
| // used for testing code generation | ||
| testImplementation group: 'junit', name: 'junit', version: junit4Version | ||
| testImplementation group: 'org.junit.platform', name: 'junit-platform-console-standalone', version: junit4PlatformVersion | ||
| testImplementation group: 'org.mockito', name: 'mockito-core', version: mockitoVersion | ||
| testImplementation group: 'org.mockito', name: 'mockito-inline', version: mockitoInlineVersion | ||
| testImplementation group: 'org.jacoco', name: 'org.jacoco.report', version: jacocoVersion | ||
|
|
||
| testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion | ||
| } | ||
|
|
||
| configurations { | ||
| all { | ||
| exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' | ||
| } | ||
| } | ||
|
|
||
| test { | ||
| if (System.getProperty('DEBUG', 'false') == 'true') { | ||
| jvmArgs '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009' | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
utbot-spring-test/src/test/kotlin/org/utbot/examples/spring/autowiring/OrderServiceTests.kt
This file contains hidden or 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,62 @@ | ||
| package org.utbot.examples.spring.autowiring | ||
|
|
||
| import org.junit.jupiter.api.Test | ||
| import org.utbot.examples.spring.utils.springAdditionalDependencies | ||
| 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, | ||
| configurations = standardSpringTestingConfigurations | ||
| ) { | ||
| @Test | ||
| fun testGetOrders() { | ||
| checkMocks( | ||
| method = OrderService::getOrders, | ||
| branches = eq(1), | ||
| { mocks, r -> | ||
| val orderRepository = mocks.singleMock("orderRepository", findAllRepositoryCall) | ||
| orderRepository.value<List<Order>?>() == r | ||
| }, | ||
| coverage = DoNotCalculate, | ||
| mockStrategy = MockStrategyApi.OTHER_CLASSES, | ||
| additionalDependencies = springAdditionalDependencies, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testCreateOrder() { | ||
| checkMocksWithExceptions( | ||
| method = OrderService::createOrder, | ||
| // TODO: replace with `branches = eq(1)` after fix of https://github.com/UnitTestBot/UTBotJava/issues/2367 | ||
| branches = ignoreExecutionsNumber, | ||
| { _: Order?, mocks, r: Result<Order?> -> | ||
| val orderRepository = mocks.singleMock("orderRepository", saveRepositoryCall) | ||
| orderRepository.value<Order?>() == r.getOrNull() | ||
| }, | ||
| coverage = DoNotCalculate, | ||
| mockStrategy = MockStrategyApi.OTHER_CLASSES, | ||
| 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?> | ||
| } |
17 changes: 17 additions & 0 deletions
17
...spring-test/src/test/kotlin/org/utbot/examples/spring/utils/SpringTestingConfiguration.kt
This file contains hidden or 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.utils | ||
|
|
||
| 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.testing.SpringConfiguration | ||
| import org.utbot.testing.TestExecution | ||
|
|
||
| val standardSpringTestingConfigurations: List<SpringConfiguration> = listOf( | ||
| SpringConfiguration(CodegenLanguage.JAVA, ParametrizedTestSource.DO_NOT_PARAMETRIZE, TestExecution) | ||
| ) | ||
|
|
||
| val springAdditionalDependencies: Array<Class<*>> = arrayOf( | ||
| JpaRepository::class.java, | ||
| PagingAndSortingRepository::class.java, | ||
| ) |
This file contains hidden or 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,36 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Configuration> | ||
| <Appenders> | ||
| <File name="FrameworkAppender" | ||
| append="false" | ||
| fileName="logs/framework.log" | ||
| filePattern="logs/framework-%d{MM-dd-yyyy}.log.gz" | ||
| ignoreExceptions="false"> | ||
|
|
||
| <PatternLayout pattern="%d{HH:mm:ss.SSS} | %-5level | %-25c{1} | %msg%n"/> | ||
| </File> | ||
|
|
||
| <Console name="Console" target="SYSTEM_OUT"> | ||
| <ThresholdFilter level="DEBUG" onMatch="NEUTRAL" onMismatch="DENY"/> | ||
| <PatternLayout pattern="%d{HH:mm:ss.SSS} | %-5level | %-25c{1} | %msg%n"/> | ||
| </Console> | ||
| </Appenders> | ||
| <Loggers> | ||
| <!-- Uncomment this logger to see path --> | ||
| <Logger name="org.utbot.engine.UtBotSymbolicEngine.path" level="debug"/> | ||
|
|
||
|
|
||
| <!-- Set this logger level to TRACE to see SMT requests, and SAT/UNSAT/UNKNOWN responses --> | ||
| <Logger name="org.utbot.engine.pc" level="debug"/> | ||
|
|
||
| <!-- Not interested in summarization logs now --> | ||
| <Logger name="org.utbot.summary" level="info"/> | ||
|
|
||
| <Logger name="soot.PackManager" level="INFO"/> | ||
|
|
||
| <Root level="debug"> | ||
| <AppenderRef ref="Console"/> | ||
| <AppenderRef ref="FrameworkAppender"/> | ||
| </Root> | ||
| </Loggers> | ||
| </Configuration> |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -40,14 +40,14 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes | |
|
|
||
| private fun runPipelinesStages(classPipeline: ClassPipeline): CodeGenerationResult { | ||
| val classUnderTest = classPipeline.stageContext.classUnderTest | ||
| val classPipelineName = | ||
| classUnderTest.qualifiedName ?: error("${classUnderTest.simpleName} doesn't have a fqn name") | ||
| val classPipelineName = classUnderTest.qualifiedName | ||
| ?: error("${classUnderTest.simpleName} doesn't have a fqn name") | ||
|
|
||
| logger | ||
| .info() | ||
| .measureTime({ "Executing code generation tests for [$classPipelineName]" }) { | ||
| CodeGeneration.verifyPipeline(classPipeline)?.let { | ||
| withUtContext(UtContext(it.stageContext.classUnderTest.java.classLoader)) { | ||
| withUtContext(UtContext(classUnderTest.java.classLoader)) { | ||
| processCodeGenerationStage(it) | ||
| } | ||
| } | ||
|
|
@@ -86,13 +86,13 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes | |
| val prefix = when (codegenLanguage) { | ||
| CodegenLanguage.JAVA -> | ||
| when (parametrizedTestSource) { | ||
| ParametrizedTestSource.DO_NOT_PARAMETRIZE -> "public void " | ||
| ParametrizedTestSource.DO_NOT_PARAMETRIZE -> "@Test" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of |
||
| ParametrizedTestSource.PARAMETRIZE -> "public void parameterizedTestsFor" | ||
| } | ||
|
|
||
| CodegenLanguage.KOTLIN -> | ||
| when (parametrizedTestSource) { | ||
| ParametrizedTestSource.DO_NOT_PARAMETRIZE -> "fun " | ||
| ParametrizedTestSource.DO_NOT_PARAMETRIZE -> "@Test" | ||
| ParametrizedTestSource.PARAMETRIZE -> "fun parameterizedTestsFor" | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.