In this section:

Introduction

Unit Test Assistant allows you to create and execute JUnit tests for Spring components: @Controller, @RestController, @Component, @Repository, and @Service. By analyzing code, UTA can generate and preconfigure runnable test templates that contain sample assertions and initial values. When adding tests for Spring components, you can choose to create:

  • Standard JUnit tests
  • Spring integration tests that utilize the Spring test framework and run in a Spring application context (see Spring MVC Test Framework for details)

Support for Spring Integration Tests

UTA supports Spring dependency injection for autowired fields, setter methods, and constructor arguments. It creates an inner configuration class to provide beans to Spring for necessary dependencies. If mocking is enabled (see Configuring Preferences), all dependencies are added as mocked beans.

Tests for Handler Methods in Spring MVC Controllers

Tests created for handler methods in @Controller and @RestController are configured using MockMvc. 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.

Spring Boot

When you create tests in a Spring Boot project, inner configuration classes are not added.

Creating Spring Tests

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 component 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 creates a standard JUnit test for the selected method–without using the Spring framework.
    - Regular Spring creates a Spring test for the selected method.
    Parameterized creates parameterized test cases–without using the Spring framework.
    Parameterized Spring creates parameterized Spring test cases for the selected method.
    - Add Spring test case(s) creates tests for multiple methods from the component.
  3. 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. See Creating Multiple Unit Tests for details.
  4. The generated test or tests include sample assertion templates and default UTA values. View the test(s) to uncomment the sample assertions and modify the values.

    Spring tests created with UTA do not include optional parameters. For example the following parameter will not be included:
    @RequestParam(value = "count", required = false)

  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.

Creating Spring Tests for Private Methods

UTA allows you to create regular and parameterized Spring tests for private methods to achieve a higher code coverage rate.

  1. Enable the Create tests for private methods option in the UTA Preferences (see Configuring Preferences).
  2. Select a private Spring component method in the editor.
  3. Choose one of the following options:
    - Click Regular private to create a regular test for the selected private method–without using the Spring framework.
    - Click Regular Spring private to create a regular Spring test for the selected private method.
    - Click Parameterized private to create a parameterized test for the selected private method–without using the Spring framework.
    - Click Parameterized Spring private to create a parameterized Spring test for the selected private method.
    - Click Add Spring test case(s) to create tests for multiple methods from the component, including the private methods.

(info) By default, the Create tests for private methods option is disabled, since the industry best practice is to test private methods by creating tests for the accessible methods that call them, rather than by creating tests for private methods directly.

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, 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.

  1. Go to Parasoft> Preferences> Unit Test Assistant.
  2. 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:

    locations={"classpath:/**/FirstContext.xml", "classpath:/**/SecondContext.xml"}


    - Reference to one or more separate Java configuration classes. Your configuration attributes may resemble the following:

    classes={FirstConfigClass.class, SecondConfigClass.class}

Example

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

  • No labels