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, and HTTP headers. 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 the Mock option is enabled (see Configuring Preferences), all controller dependencies are added as mocked beans to the inner configuration class.

To create a Spring test:

  1. Choose Parasoft> Show View> Unit Test Assistant from the Eclipse menu bar to open the Unit Test Assistant interface.
  2. 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:
    - Quick Spring creates a test for the selected method.
    - Add Spring test case(s) creates tests for multiple methods from the controller.
    - Parameterized Spring creates parameterized test cases. 
  3. Select one of the available options. If you choose Add Spring test cases(s), select the methods you want to create test cases for when prompted, and click OK.



  4. 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.
  5. 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.

 

  • No labels