In this section:
Unit Test Assistant allows you to create and execute JUnit tests for Spring controller methods that implement HTTP endpoints using Web MVC. By analyzing code, it can generate and preconfigure runnable test templates that contain sample assertions and initial values. UTA can recognize and configure settings for request body, response body, response mime types, HTTP method, path parameters, query parameters, HTTP headers and session attributes. It supports Spring dependency injection through autowired fields, setter methods, and constructor arguments.
Tests for handler methods in Spring MVC controllers are automatically run as Spring JUnit tests. The tested handler method is invoked with MockMvc, and an inner class is configured to provide beans to Spring for necessary dependencies. If mocking is enabled (see Configuring Preferences), all controller dependencies are added as mocked beans to the inner configuration class.
Creating Spring Tests
To create a Spring test:
- Choose Parasoft> Show View> Unit Test Assistant from the Eclipse menu bar to open the Unit Test Assistant interface.
- Select a Spring controller in the editor. UTA will analyze your code and display one or more options that are available for the selection. The following options may be displayed:
- Regular Spring creates a test for the selected method.
- Parameterized Spring creates parameterized test cases.
- Add Spring test case(s) creates tests for multiple methods from the controller.
- Select one of the available options. If you choose Add Spring test case(s), select the methods you want to create test cases for when prompted, and click OK.
- The test or tests generated include sample assertion templates and default UTA values. View the test(s) to uncomment the sample assertions and modify the values.
- Run the test with UTA to collect coverage information and recommendations for possible improvements (see Executing Unit Tests with Unit Test Assistant). To facilitate finding handler method calls in the execution flow tree, most internal Spring calls are hidden.
Configuring Spring tests with the ContextConfiguration Annotation
A Spring unit test requires the beans to be available when the test is executed to provide necessary dependencies. By default, UTA defines the available beans directly in the test case by creating and populating an inner configuration class with the required Bean methods. (Note: When you create tests in a Spring Boot project, a configuration class is not created unless mocking is enabled, since Spring Boot can automatically scan for dependencies).
Alternatively, you can configure your Spring tests with the @ContextConfiguration annotation by providing the reference to XML configuration files or separate Java configuration classes.
- Go to Parasoft> Preferences> Unit Test Assistant.
Complete the ContextConfiguration attributes for Spring tests field with one of the following:
- Reference to one or more XML configuration files. Your configuration attributes may resemble the following:
- Reference to one or more separate Java configuration classes. Your configuration attributes may resemble the following:
You can configure the ContextConfiguration attributes for Spring tests option with a reference to two XML configuration files:
The generated Spring unit test will include a ContextConfiguration annotation with the configured attributes:
For more details on the ContextConfiguration annotation, see the spring documentation https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html