-
Notifications
You must be signed in to change notification settings - Fork 46
Testing Spring based code
UnitTestBot Java can now generate tests for Spring-based code.
UnitTestBot Java defines if your code is based on the Spring framework and provides you with necessary options right in the dialog window of the standard UnitTestBot Java plugin.
You may choose from the three approaches to generating Spring tests:
- standard unit tests that mock the Spring-related code;
- Spring-guided unit tests that get use of Spring context but still check the behavior of the application unit in isolation;
- and integration tests that validate interactions between components.
For each class under test, select an appropriate type of test generation. In the sections below, you can find recommendations on how to choose the generation type.
NOTE UnitTestBot Java with Spring support uses symbolic execution to generate unit tests (both standard and Spring-guided ones), so it may be not so efficient for:
- multithreading programs,
- functions with calls to external libraries,
- processing large collections.
Testing frameworks supported:
Testing framework | Support |
---|---|
JUnit4 | ✔️ |
JUnit5 | ✔️ |
TestNG | ❌ |
Standard unit tests are the tests for isolated components. UnitTestBot Java mocks all the Spring environmental dependencies using Mockito.
This test generation type does not process the Spring context of the original application.
This approach is most useful in these cases:
- if the component has meaningful logic,
- for testing the Service or DAO layer of a Spring application,
- if you need the fastest and the easiest (“one-click”) way of generating tests.
- Open your Spring project in IntelliJ IDEA.
- Choose the required package, class or set of classes in the Project tool window.
Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U. - Alternatively, in the Editor, place the caret at the required class or method.
Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot. - In the Generate Tests with UnitTestBot window, for Spring configuration, choose No configuration.
For setting the rest of the options, refer to Fine-tune test generation.
- Press Generate Tests or Generate and Run.
To let unit tests get use of injected dependencies, UnitTestBot Java:
- starts executing a Spring application based on a user configuration,
- gets information about the bean types from bean definitions (already constructed but not initialized yet),
- destroys these definitions and crashes the application context gracefully.
Dependencies in Spring applications are often injected via interfaces, each having only one implementation, so UnitTestBot Java uses it to generate the test. If an interface has more than one implementation, UnitTestBot Java mocks this dependency.
To guide this type of test generation, select a configuration:
Configuration approaches | Support |
---|---|
.xml file | ✔️ |
Java annotation | ✔️ |
automated configuration for Spring Boot code | ✔️ |
This approach is most useful in these cases:
- when you need to reduce the number of mocks in generated tests,
- when you need to use real object types instead of interfaces.
NOTES
- It may provide not the maximum line coverage as it is aimed at covering the lines relevant to the chosen configuration.
- The generation process may exceed the requested timeout due to symbolic execution limitations.
- Open your Spring project in IntelliJ IDEA.
- Choose the required package, class or set of classes in the Project tool window.
Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U. - Alternatively, in the Editor, place the caret at the required class or method.
Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot. - In the Generate Tests with UnitTestBot window, for Spring configuration, choose the required one.
- For Tests type, choose Unit tests.
-
For setting the rest of the options, refer to Fine-tune test generation.
-
Press Generate Tests or Generate and Run.
For integration testing, UnitTestBot Java invokes real component interactions and mocks nothing. It uses a fuzzing technique (not symbolic execution) to mutate the initial state of the object under test.
WARNING
We strictly recommend creating a separate testing configuration: never generate tests using the development or production configuration.
NOTES
- Integration test generation may take longer than generating unit tests.
- If the method under test contains calls to microservices, you need to start that microservice manually.
- Open your Spring project in IntelliJ IDEA.
- Choose the required package, class or set of classes in the Project tool window.
Right-click and choose Generate Tests with UnitTestBot or press Alt+Shift+U. - Alternatively, in the Editor, place the caret at the required class or method.
Press Alt+Shift+U or right-click and choose Generate > Tests with UnitTestBot. - In the Generate Tests with UnitTestBot window, for Spring configuration, choose the required one.
- For Tests type, choose Integration tests.
- For Active profile(s), enter the profile name:
-
For setting the rest of the options, refer to Fine-tune test generation.
-
Press Generate Tests or Generate and Run.
For the Microservice layer, UnitTestBot Java creates a contextLoads
test: it checks if the Spring application
context has started normally.
Failing test means that the application is not properly configured, so the other test failures are not related to regression in the tested code. If loading context fails, the test contains a commented exception type, a message, and a stack trace to help a developer investigate the problem.
- Check system requirements
- Install or update plugin
- Generate tests with default configuration
- Fine-tune test generation
- Get use of test results
(redirect to /docs in main repo)
- Contributing guide
- Developer guide
- Naming and labeling conventions
- Interprocess debugging
- Interprocess logging
- UnitTestBot overall architecture
- Android Studio support
- Assumption mechanism
- Choosing language-specific IDE
- Code generation and rendering
- Fuzzing Platform (FP) Design
- Instrumented process API: handling errors and results
- UnitTestBot JavaScript plugin setup
- Night statistics monitoring
- RD for UnitTestBot
- Sandboxing
- UnitTestBot settings
- Speculative field non-nullability assumptions
- Symbolic analysis of static initializers
- Summarization module
- Taint analysis
- UnitTestBot decomposition
- UtUtils class
- UnitTestBot Family changes