In this section:

Introduction

Unit Test Assistant facilitates creating mock objects that isolate a test from its dependencies. When a test is generated, UTA can automatically create mocks, which simulate the behavior of real complex objects, such as external interfaces, that cannot be incorporated into a test. This allows you to run your tests in isolation - without having to add excessive code to your test. 

Creating Unit Tests with Mocks

  1. Ensure that the options for creating mocks have been configured (see Configuring Mocking Options).
  2. Create a new unit test with UTA  (see Creating a Basic Unit TestCreating Test Suites or Creating a Parameterized Unit Test). By analyzing source code, UTA will detect complex objects that cannot be incorporated and add a mock object to the test.



  3. Execute the test and view the results in the UTA interface (see Executing Unit Tests with Unit Test Assistant).
        
    The Recommendations view displays information about interactions with mock objects.

      
    Clicking the Highlight icon helps you view these interactions in execution flow displayed in the left column. 
     

    The nodes marked in brown indicate the mock object. The nodes in bold indicate interactions with the mock object. Double-clicking the nodes navigates the test code.

UTA does not automatically generate mocks for the following methods from the java.lang.Object class:

  • boolean equals(Object obj)
  • int hashCode()
  • Class<?> getClass()
  • String toString()

Customizing Mocks

Mocks created with UTA include default values you may want to modify. 

  1. Click the Mock it action link to extend the test with elements required for mock modification.
     

     
    (info) The Mock it action applies to local variables declared in test methods or helper methods, as well as fields annotated with @Mock.
     
  2. Modify the mock with values that meet your testing strategy.
     

Configuring Mock Initialization

By default, UTA initializes mocks by declaring and initializing the object under test and its dependencies within the test method.  If you create test cases in bulk, you can configure UTA to initialize the object under test and all of its dependencies at the class level by using the @InjectMocks annotation for the object under test and @Mock annotations for all of its dependencies. To achieve this, enable the the Use @InjectMocks option in the the Unit Test Assistant - Add test case(s) dialog (see Creating Test Suites).

With this option enabled, UTA will:

  1. Declare the object under test as a test-class level field with the @InjectMocks annotation.
  2. Add a field annotated with @Mock for any field in the object under test (and its super-classes) that is:
    - declared as a complex object, interface, or abstract class.
    - used in the method under test or a sub-call of the method under test.
  3. Add a setupMocks method using the @Before Junit annotation, which initializes the object under test and its dependencies based on the annotations that were added. 

The @InjectMocks annotation is not supported for:

  • Spring integration tests, which use the container/configuration model to initialize mocks.
  • Multiple fields that have the same type.
  • Some dependency types, such as collections, maps, types for which a factory method exists, or primitive types.

The @InjectMocks annotation is available for Mockito 1.8.3 and later; see https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/InjectMocks.html for details.

Mocking Static Methods

You can configure UTA to create mocks for specific static methods.

  1. Go to Parasoft> Preferences> Unit Test Assistant> Mocking in your IDE menu bar.
  2. Select the Enable check box to enable creating unit tests with mocks.
  3. Enable the Enable static method and constructor mocking (Mockito inline 3.5+ required) option in the Recommendations section (see Configuring Mocking Options for details).
  4. Complete the Static methods and constructors to mock table with a list of static methods and/or constructors that you want to be mocked. Click New and provide a qualified method name or pattern using wildcards. UTA will mock only the static methods that are specified in the table. 
        

     
    Alternatively, you can add static methods to the list after you execute your tests (see Executing Unit Tests with Unit Test Assistant for details). Right-click a node that represents a static method in the execution flow and choose the Add Mockable Method Pattern option. Selecting the Mock Static Method option will update your code to mock the selected method call, but the method will not be added to the table; see Executing Unit Tests with Unit Test Assistant for details.


    If UTA detects invocations of the specified static methods in the tested code during test creation, mocks will be included in the generated test templates (see Creating a Basic Unit Test).

    See Mockito Static and Constructor Mocking Limitations for details about cases which are not supported by Mockito.

  5. Run your unit tests with UTA (see Executing Unit Tests with Unit Test Assistant). UTA will detect calls to the specified static methods and display the Recommendations that allow you automatically create mocks.
  6. Click the Mock static call action link.

     
    The specified static method will be mocked with the with the mockStatic method.


  • No labels