LogoLogo
diffblue.comFree trial
  • Discover Diffblue Cover
  • Get Started
    • What is Diffblue Cover?
    • Get started
      • Free trial
      • Get started - Cover Plugin
      • Get started - Cover CLI
      • Get started - Cover Pipeline
      • Get started - Cover Reports
    • Specs & Reqs
    • Reference Deployments
    • Licensing
      • Online license activation
      • Offline license activation
      • Diffblue License Manager
    • Update Cover
    • FAQs
    • Diffblue Learning
      • Get started
        • Just the basics
        • Free trial
        • Cover Plugin (IDE)
        • Cover CLI (Command Line)
      • Developer
        • Unit tests (IDE)
        • Unit tests (CLI)
        • Test coverage
      • DevOps
        • GitHub
        • GitLab
        • Other CI
      • Administrator
        • Admin - IntelliJ
        • Admin - CLI
        • Admin - Reports
      • Test coverage
        • Developer
        • Senior developer
        • Cover Reports Administrator
  • EVALUATION & ONBOARDING
    • Proof of Value
    • Jumpstart
      • Prerequisites for onboarding
      • Phase 1: Up and running
        • Module 1: Create your Cover unit test baseline
        • Module 2: Cover Pipeline CI integration
      • Phase 2: Developer productivity
        • Module 3: Getting started using Cover
        • Module 4: Introduction to Cover Reports
      • Phase 3: Advanced topics
        • Module 5: Speed up your test execution
        • Module 6: Getting more from Cover
  • Features
    • Cover Plugin
      • Writing tests
        • Gutter icons
        • Menu options
        • Run configurations
        • Cover Plugin tool window
        • Test Review
        • Test examples
        • Creating partial tests
        • Creating skeleton tests
        • Covering all enum values
        • Test insertion order
        • Diffblue Sandbox
        • Environment Check Cache
      • Project configuration
        • General dependencies
        • Test framework dependencies
      • Cover Plugin settings
        • Test Naming
        • Test Formatting
        • Spring configuration options
        • Method Annotations
        • Test Directory
        • Reset Cover Plugin settings
      • Cover Plugin admin
        • Core Maintenance
        • Cover Plugin toolbar menu
        • Cover Plugin status bar widget
        • Telemetry
        • Memory management
        • Using SonarQube with Cover Plugin
        • Log files
        • Troubleshooting
    • Cover CLI
      • Writing tests
        • Command summary
        • Test examples
        • Creating partial tests
        • Customizing test inputs
        • Customizing test setup
        • Test naming
        • Test formatting
        • Test insertion order
        • Patch files
        • Diffblue Sandbox
        • Operational behaviors
        • Test validation
      • Project configuration
        • Preflight checks
        • General dependencies
        • Test framework dependencies
        • Compiling your project successfully
          • Building a Maven project
          • Building a Gradle project
        • Configuring Cover to work with your project's build system
          • Quick guide
          • Configuration file in detail
          • Getting Started with Ant Projects
          • Default configuration
        • Mocking using Mockito
        • Spring profiles
        • Runtime environment
      • Commands & Arguments
        • Environment configuration for CI
        • Packages, classes, and methods
        • Quoting command line arguments
        • Argument files
      • Cover CLI admin
        • Core Maintenance
        • Telemetry
        • Memory management
        • Using Cover CLI in Eclipse
        • Using SonarQube with Cover CLI
        • Log files
        • Troubleshooting
      • Environment Configuration
    • Cover Pipeline
      • Cover Pipeline for CI
        • Quick Start - General
        • Quick Start - Jenkins
        • Quick Start - Azure Pipelines
        • Quick Start - AWS Codebuild
        • Integrating Diffblue Cover into CI on pull requests
        • Installation - VMs or CI run
      • Cover Pipeline for GitLab
        • Introduction
        • Installation and initial setup
        • GitLab workflow
        • Configuration
        • Troubleshooting
        • Related topics
      • Cover Pipeline for GitHub
        • Introduction
        • Installation and initial setup
        • GitHub workflow
        • Configuration
        • Troubleshooting
        • Related topics
    • Cover Reports
      • Cover Reports Contributor
        • Java project config (JaCoCo)
        • Generate and upload reports bundles
        • Authenticated uploads
      • Cover Reports User
        • Navigation
        • Dashboards
        • Telemetry data
        • Export API
        • Considerations
      • Cover Reports Administrator
        • Install and update Cover Reports
        • Configuration options
        • Database backup
        • SSO with Cover Reports
        • Uninstall Cover Reports
    • Cover Optimize
      • Get started - Cover Optimize
      • Cover Optimize & Gradle
      • Cover Optimize & Maven
      • Patch files
    • Cover Refactor
      • Get started - Cover Refactor
    • Cover Annotations
      • Mocking Annotations
      • Custom Input Annotations
      • Interesting Value Annotations
    • Output Codes
      • E - Environment Codes
      • L - License Codes
      • R - Reason Codes
      • T - Testability Codes
      • V - Validation Codes
      • Working with output codes
        • Working with code E020
        • Working with codes E057 to E065
        • Working with code E085
        • Working with code R005
        • Working with code R006
        • Working with code R008
        • Working with code R011
        • Working with code R012
        • Working with code R013
        • Working with code R026
        • Working with code R031
        • Working with code V003
    • Tutorials
      • How to measure test quality
      • How to improve code coverage
      • How to test a new feature
      • How to find regressions
      • How to use Diffblue Cover in test driven development (TDD)
      • How to write tests for Kotlin projects
      • Examples of tests created by Diffblue Cover
      • Best practices for testing private methods
  • Updates & Upgrades
    • Update Cover
    • Cover Editions
    • What's new
    • Release archive
      • 2025-05-01
      • 2025-04-02
      • 2025-04-01
      • 2025-03-02
      • 2025-03-01
      • 2025-02-02
      • 2025-02-01
      • 2025-01-02
      • 2025-01-01
      • 2024-12-02
      • 2024-12-01
      • 2024-11-02
      • 2024-11-01
      • 2024-10-02
      • 2024-10-01
      • 2024-09-02
      • 2024-09-01
      • 2024-08-02
      • 2024-08-01
      • 2024-07-04
      • 2024-07-03
      • 2024-07-01
      • 2024-06-02
      • 2024-06-01
      • 2024-05-02
      • 2024-05-01
      • 2024-04-02
      • 2024-04-01
      • 2024-03-02
      • 2024-03-01
      • 2024-02-02
      • 2024-02-01
      • 2024-01-02
      • 2024-01-01
      • 2023-12-02
      • 2023-12-01
      • 2023-11-03
      • 2023-11-02
      • 2023-11-01
      • 2023-10-02
      • 2023-10-01
      • 2023-09-02
      • 2023-09-01
      • 2023-08-02
      • 2023-08-01
      • 2023-07-03
      • 2023-07-02
      • 2023-07-01
      • 2023-06-02
      • 2023-06-01
      • 2023-05-02
      • 2023-05-01
      • 2023-04-02
      • 2023-04-01
      • 2023-03-02
      • 2023-03-01
      • 2023-02-02
      • 2023-02-01
      • 2023-01-02
      • 2023-01-01
      • 2022-12-02
      • 2022-12-01
      • 2022-11-02
      • 2022-11-01
      • 2022-10-03
      • 2022-10-02
      • 2022-10-01
      • 2022-09-02
      • 2022-09-01
      • 2022-08-05
      • 2022-08-03
      • 2022-08-02
      • 2022-08-01
      • 2022-07-02
      • 2022-07-01
      • 2022-06-02
      • 2022-06-01
      • 2022-05-02
      • 2022-05-01
      • 2022-04-02
      • 2022-04-01
      • 2022-03-02
      • 2022-03-01
      • 2022-02-02
      • 2022-02-01
      • 2022-01-02
      • 2022-01-01
      • 2021-06-02
      • 2021-02-01
  • Legal
    • Diffblue Legal
      • Diffblue End User License Agreement (EULA)
      • Cover Third Party Notices and/or Licenses
      • Privacy Notice
Powered by GitBook
On this page
  • Mocking static methods
  • Configuration
  • Usage
  • Caveat
  • Mocking constructors
  • Configuration
  • Usage

Was this helpful?

Export as PDF
  1. Features
  2. Cover CLI
  3. Project configuration

Mocking using Mockito

PreviousDefault configurationNextSpring profiles

Last updated 1 year ago

Was this helpful?

Mocking static methods

Since Mockito has provided the capability to mock static method invocations (AKA static mocking).

The method is the entrypoint for the mocking of class objects and their static methods. In the example below the public static method Currency.getInstance is mocked:

  @Test
  public void testCurrency() {
    Currency usd = Currency.getInstance("USD");
    try (MockedStatic<Currency> mockedStatic = mockStatic(Currency.class)) {
      mockedStatic.when(() -> Currency.getInstance(anyString())).thenReturn(usd);

      Currency eur = Currency.getInstance("EUR");
      assertEquals("USD", eur.getCurrencyCode());
      mockedStatic.verify(() -> Currency.getInstance(anyString()));
    }
  }

As you can see, the API for static mocks is slightly different from that used for creating instance mocks. It is also recommended to execute static mocks within a try-with-resources statement.

Configuration

Static mocking is only available if the inline mock maker is in your project. Since version 5.0.0 of Mockito, the inline mock maker is the default and no action is needed. For prior versions of Mockito, instead of using the mockito-core artifact, include the mockito-inline artifact in your project to enable the inline mock maker. The example below shows detail from a Maven pom.xml:

    <dependency>
      <groupId>org.mockito</groupId>
      <!--<artifactId>mockito-core</artifactId>-->
      <artifactId>mockito-inline</artifactId>
      <version>4.6.1</version>
      <scope>test</scope>
    </dependency>

Usage

To support the mocking of static methods the --mock-static option has been added to Diffblue Cover. This allows the user to suggest the names of classes whose static methods should be mocked when unit testing. Diffblue Cover will mock the static methods of these suggested classes, and if those mocks prove valuable for test coverage they will be included in the resulting tests.

For example, in the example command line below, the user is suggesting that it might be of utility to mock both the Foo class and the Bar class when creating tests for the class FooBarUser:

dcover create com.diffblue.FooBarUser --mock-static=com.diffblue.Foo,com.diffblue.Bar

Diffblue Cover will issue warnings if the --mock-static option is used in a project which does not support static mocking - it will ignore the option and continue with test creation.

Caveat

The inline mock maker is an optional feature of Mockito before version 5.0.0.

Note: We recommend against mocking static methods of classes in the standard library or classes used by custom class loaders used to executed the block with the mocked class. A mock maker might forbid mocking static methods of know [sic] classes that are known to cause problems. Also, if a static method is a JVM-intrinsic, it cannot typically be mocked even if not explicitly forbidden.

The error messages issued by Mockito.mockStatic can be broad and confusing. The static mocking of low-level standard library classes can be unreliable and have unpredictable effects. It may take some experimentation to fine-tune use of this powerful feature.

Mocking constructors

  public static long currentTime() {
    return new Date().getTime();
  }

  @Test
  public void testCurrentTime() {
    // Arrange
    try (MockedConstruction<Date> dateMocked = mockConstruction(Date.class, (mock, context) -> {
      when(mock.getTime()).thenReturn(1_000_000_000_000L);
    })) {
      // Act
      final long time = currentTime();
      // Assert
      Assertions.assertEquals(1_000_000_000_000L, time);
    }
  }

As you can see, the API for mocked construction is different from that used for creating instance mocks. Similar to static mocking, it is recommended to mock constructors within a try-with-resources statement.

Configuration

    <dependency>
      <groupId>org.mockito</groupId>
      <!--<artifactId>mockito-core</artifactId>-->
      <artifactId>mockito-inline</artifactId>
      <version>4.11.0</version>
      <scope>test</scope>
    </dependency>

Usage

To support the mocking of constructors the --mock-construction option has been added to Diffblue Cover. This allows the user to suggest the names of classes whose constructors should be mocked when unit testing. Diffblue Cover will mock the constructors of these suggested classes, and if those mocks prove valuable for test coverage they will be included in the resulting tests.

For example, in the example command line below, the user is suggesting that it may be useful to mock both the Foo constructors and the Bar constructors when creating tests for the class FooBarUser:

dcover create com.diffblue.FooBarUser --mock-construction=com.diffblue.Foo,com.diffblue.Bar

Diffblue Cover will issue warnings if the --mock-construction option is used in a project which does not support mocked construction - it will ignore the option and continue with test creation.

Mockito includes this note in the JavaDoc for .

Since Mockito has provided the capability to mock constructor method invocations.

The method is the entrypoint for the mocking of constructors. In the example below the Date constructor is mocked:

Construction mocking is only available if the inline mock maker is in your project. To enable it, instead of using the mockito-core artifact, include the mockito-inline artifact in your project. The example below shows a detail from a Maven pom.xml:

version 3.4.0
Mockito.mockStatic
enabled
Mockito.mockStatic
version 3.5.0
mockConstruction
enabled