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
  • eLearning
  • Overview
  • Find the root cause
  • Run/Debug the test
  • Missing input values
  • Unsettable fields
  • Inputs that are hard to build: mock
  • Inputs that are hard to build: refactor
  • Dependency on environment variables

Was this helpful?

Export as PDF
  1. Features
  2. Output Codes
  3. Working with output codes

Working with code R013

PreviousWorking with code R012NextWorking with code R026

Last updated 1 year ago

Was this helpful?

R013 - No inputs found that don't throw a trivial exception

eLearning

Overview

If you receive the output code R013, this means that Diffblue Cover cannot write tests for a method because it is unable to find inputs to provide to the method that don't cause trivial exceptions, such as NullPointerException. Please consider the parameters of the method under test, along with the class variables and instance variables it uses to ensure that it is easily possible to assign reasonable values for these quantities that do not cause trivial exceptions to be thrown.

A possible cause of an R013 is that the method under test uses a private member with an unset or bad value, as is shown in the example below:

public class SomeClass() {
  private Runnable value;
  // ...
  public int runSomething() {
    // ...
    value.run();
    // ...
  }
}

There are three solutions to this scenario - ensure that value is initialized with a proper value, make value public, or provide a getter and a setter for value.

Ensure that your method is testable. A good way to do this is to write a test yourself using the available constructor and setter methods.

If you have ensured that the method you want to analyze is testable, and are still getting an R013 code, you can further investigate the cause of the issue.

Find the root cause

A good tool to investigate coverage issues is to get a partial test from Diffblue Cover.

In Cover Plugin for IntelliJ, partial tests are available by default, all you have to do is click on the button to write tests.

In the CLI, pass in the --keep-partial-tests option.

Run/Debug the test

Running the partial test (e.g. with a debugger) can help you find the cause of the issue. Based on what you see, one of the following scenarios may apply.

Missing input values

Consider the following example where we want tests for the setType() method:

package io.diffblue.example;

import java.util.HashMap;

public class SomeClass {

  enum Type {A, B, C}

  private static final HashMap<Integer, Type> TYPE_MAP = new HashMap<>();

  static {
    TYPE_MAP.put(1111, Type.A);
    TYPE_MAP.put(1212, Type.B);
    TYPE_MAP.put(1313, Type.C);
  }

  public String setType(int shiftedTypeId) {
    Type type = TYPE_MAP.get(shiftedTypeId - 1); // Shift the id to confuse Diffblue Cover
    return type.toString();                      // <-- line 19
  }

}

Diffblue Cover will not write a full test and will render an R013 instead. To find out what the issue is, let us get the partial tests as explained earlier. In our case we get one partial test:

  @Test
  @Disabled("TODO: Complete this test")
  void testDiffblueSetType() {
    // TODO: Complete this test.
    //   Reason: R013 No inputs found that don't throw a trivial exception.
    //   Diffblue Cover tried to run the arrange/act section, but the method under
    //   test threw
    //   java.lang.NullPointerException
    //       at io.diffblue.example.SomeClass.setType(SomeClass.java:19)
    //   In order to prevent setType(int)
    //   from throwing NullPointerException, add constructors or factory
    //   methods that make it easier to construct fully initialized objects used in
    //   setType(int).
    //   See https://diff.blue/R013 to resolve this issue.

    // Arrange and Act
    (new SomeClass()).setType(123);
  }

The NullPointerException on line 19 tells us that Diffblue Cover was unable to find a shiftedTypeId that matches one of the keys in the map.

We provide them manually by using custom inputs. In our case, we can create a DiffblueRules.yml file as below and place it at the root of the module:

int:
  - immediate: 1112
    parameter: "shiftedTypeId"

("immediate" stands for constant primitive or String)

This will prompt Diffblue Cover to use 1112 as a value for parameters named shiftedTypeId and thus give us a test that does not throw a NullPointerException:

  @Test
  void testDiffblueSetType() {
    // Arrange, Act and Assert
    assertEquals("A", (new SomeClass()).setType(1112));
  }

Unsettable fields

In this trivial example, there is no way to set name to a non-null value:

package io.diffblue.example;

public class Unsettable {

  private String name;

  public boolean isNameABC() {
    return name.equals("ABC");
  }
}

This can be fixed by providing constructors or setters that assign that field:

  public Unsettable(String name) {
    this.name = name;
  }

  public void setName(String name) {
    this.name = name;
  }

Inputs that are hard to build: mock

If some inputs to the method under test are difficult to build, perhaps because they require complex configuration or need to read some external resources, it might be useful to mock those. Diffblue Cover automatically mocks some objects, but it might be useful to force-mock some specific classes in some cases.

Assuming that BlackBox is difficult to build in the example below:

package io.diffblue.example;

public class BlackBoxSafety {

  public static boolean isSafe(BlackBox blackBox) {
    if (blackBox.getSafetyLevel().equals("SAFE")) {
      return true;
    }
    return false;
  }
}

the CLI provides an option to force-mock a class: --mock=io.diffblue.example.BlackBox which allows Diffblue Cover to produce full coverage without having to build BlackBox:

  @Test
  void testIsSafe() {
    // Arrange
    BlackBox blackBox = mock(BlackBox.class);
    when(blackBox.getSafetyLevel()).thenReturn("Safety Level");

    // Act and Assert
    assertFalse(BlackBoxSafety.isSafe(blackBox));
    verify(blackBox).getSafetyLevel();
  }

  @Test
  void testIsSafe2() {
    // Arrange
    BlackBox blackBox = mock(BlackBox.class);
    when(blackBox.getSafetyLevel()).thenReturn("SAFE");

    // Act and Assert
    assertTrue(BlackBoxSafety.isSafe(blackBox));
    verify(blackBox).getSafetyLevel();
  }

Inputs that are hard to build: refactor

For better results, an alternative to mocking is to refactor the method under test so that the logic can be tested independently of the problematic input.

Take the following example of a method that extracts comments from a file:

  public static List<String> commentLines(File f) throws IOException {
    List<String> commentLines = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(f))) {
      String line;
      while ((line = br.readLine()) != null) {
        if (line.startsWith("#")) {
          commentLines.add(line);
        }
      }
    }
    return commentLines;
  }

Since Diffblue Cover cannot create file resources, no complete test can be produced, but by separating the logic between the file reading and the filtering as follows:

  public static List<String> commentLinesRefactored(List<String> strings) {
    return strings.stream().filter(line -> line.startsWith("#")).collect(Collectors.toList());
  }

  public static List<String> commentLinesRefactored(File f) throws IOException {
    List<String> lines = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(f))) {
      String line;
      while ((line = br.readLine()) != null) {
        lines.add(line);
      }
    }
    return commentLinesRefactored(lines);
  }

we can get unit tests for the method that does not rely on a file resource.

Note that it is also possible to access a property file as a resource using custom inputs.

Dependency on environment variables

This example will yield an R013 because getProperty() will return null unless the system property is set:

package io.diffblue.example;

public class Property {
  public int propertyLength() {
    return System.getProperty("theProperty").length();
  }
}

In Cover CLI, this can be done using the -D option (see Commands & Arguments) e.g. -DtheProperty=something. In Cover Plugin, environment variables can be set in the run configuration.