-
Notifications
You must be signed in to change notification settings - Fork 46
Extend Java API for Spring-aware generation #2614
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
denis-fokin
merged 1 commit into
main
from
denis-fokin/Java-API-for-Spring-aware-generation
Oct 17, 2023
Merged
Changes from all commits
Commits
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
1,001 changes: 371 additions & 630 deletions
1,001
utbot-framework-test/src/test/java/org/utbot/examples/manual/UtBotJavaApiTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
5 changes: 5 additions & 0 deletions
5
utbot-spring-sample/src/main/java/org/utbot/examples/spring/app/MyService.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,5 @@ | ||
package org.utbot.examples.spring.app; | ||
|
||
public interface MyService { | ||
String getName(); | ||
} |
11 changes: 11 additions & 0 deletions
11
utbot-spring-sample/src/main/java/org/utbot/examples/spring/app/MyServiceImpl.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,11 @@ | ||
package org.utbot.examples.spring.app; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class MyServiceImpl implements MyService { | ||
@Override | ||
public String getName() { | ||
return "impl"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
utbot-spring-sample/src/main/java/org/utbot/examples/spring/app/MyServiceUser.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,18 @@ | ||
package org.utbot.examples.spring.app; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class MyServiceUser { | ||
private final MyService myService; | ||
|
||
@Autowired | ||
public MyServiceUser(MyService myService) { | ||
this.myService = myService; | ||
} | ||
|
||
public String useMyService() { | ||
return myService.getName(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
utbot-spring-sample/src/main/java/org/utbot/examples/spring/app/SpringExampleApp.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,7 @@ | ||
package org.utbot.examples.spring.app; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringExampleApp { | ||
} |
105 changes: 105 additions & 0 deletions
105
utbot-spring-test/src/test/java/org/utbot/api/java/SpringUtBotJavaApiTest.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,105 @@ | ||
package org.utbot.api.java; | ||
|
||
import org.utbot.examples.spring.app.MyServiceImpl; | ||
import org.junit.jupiter.api.Test; | ||
import org.utbot.examples.spring.app.MyServiceUser; | ||
import org.utbot.examples.spring.app.SpringExampleApp; | ||
import org.utbot.examples.spring.config.pure.ExamplePureSpringConfig; | ||
import org.utbot.external.api.UtBotJavaApi; | ||
import org.utbot.external.api.UtBotSpringApi; | ||
import org.utbot.framework.codegen.domain.ForceStaticMocking; | ||
import org.utbot.framework.codegen.domain.Junit4; | ||
import org.utbot.framework.codegen.domain.MockitoStaticMocking; | ||
import org.utbot.framework.codegen.domain.ProjectType; | ||
import org.utbot.framework.context.ApplicationContext; | ||
import org.utbot.framework.context.spring.SpringApplicationContext; | ||
import org.utbot.framework.plugin.api.*; | ||
import org.utbot.framework.util.Snippet; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
|
||
import static org.utbot.framework.plugin.api.MockFramework.MOCKITO; | ||
import static org.utbot.framework.util.TestUtilsKt.compileClassFile; | ||
|
||
public class SpringUtBotJavaApiTest extends AbstractUtBotJavaApiTest { | ||
|
||
/** | ||
* We are using the environment to check spring generation the only difference in the data supplied by the argument | ||
* @param applicationContext returns Spring settings to run the generation with | ||
*/ | ||
private void supplyConfigurationAndRunSpringTest(ApplicationContext applicationContext) { | ||
|
||
UtBotJavaApi.setStopConcreteExecutorOnExit(false); | ||
|
||
String classpath = getClassPath(MyServiceImpl.class); | ||
String dependencyClassPath = getDependencyClassPath(); | ||
|
||
Method getNameMethod = getMethodByName( | ||
MyServiceImpl.class, | ||
"getName" | ||
); | ||
|
||
Method useMyServiceMethod = getMethodByName( | ||
MyServiceUser.class, | ||
"useMyService" | ||
); | ||
|
||
List<UtMethodTestSet> myServiceUserTestSets = UtBotJavaApi.generateTestSetsForMethods( | ||
Collections.singletonList(useMyServiceMethod), | ||
MyServiceUser.class, | ||
classpath, | ||
dependencyClassPath, | ||
MockStrategyApi.OTHER_PACKAGES, | ||
60000L, | ||
applicationContext | ||
); | ||
|
||
String generationResult = UtBotJavaApi.generateTestCode( | ||
Collections.emptyList(), | ||
myServiceUserTestSets, | ||
GENERATED_TEST_CLASS_NAME, | ||
classpath, | ||
dependencyClassPath, | ||
MyServiceUser.class, | ||
ProjectType.Spring, | ||
Junit4.INSTANCE, | ||
MOCKITO, | ||
CodegenLanguage.JAVA, | ||
MockitoStaticMocking.INSTANCE, | ||
false, | ||
ForceStaticMocking.DO_NOT_FORCE, | ||
MyServiceUser.class.getPackage().getName(), | ||
applicationContext | ||
); | ||
|
||
Snippet snippet = new Snippet(CodegenLanguage.JAVA, generationResult); | ||
compileClassFile(GENERATED_TEST_CLASS_NAME, snippet); | ||
} | ||
|
||
@Test | ||
public void testUnitTestWithoutSettings() { | ||
supplyConfigurationAndRunSpringTest(UtBotSpringApi.createSpringApplicationContext( | ||
SpringSettings.AbsentSpringSettings, | ||
SpringTestType.UNIT_TEST, | ||
Collections.emptyList())); | ||
} | ||
|
||
@Test | ||
public void testUnitTestWithSettings() { | ||
SpringConfiguration configuration = | ||
UtBotSpringApi.createJavaSpringConfiguration(SpringExampleApp.class); | ||
|
||
SpringSettings springSettings = | ||
new SpringSettings.PresentSpringSettings(configuration, Arrays.asList("test1", "test2")); | ||
|
||
ApplicationContext applicationContext = UtBotSpringApi.createSpringApplicationContext( | ||
springSettings, | ||
SpringTestType.UNIT_TEST, | ||
Arrays.asList(getDependencyClassPath().split(File.pathSeparator)) | ||
); | ||
|
||
supplyConfigurationAndRunSpringTest(applicationContext); | ||
} | ||
} |
Oops, something went wrong.
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.