Test examples

In this article we show both the input source and the Cover written tests for code of varied complexity accompanied by a detailed narrative to help you further understand tests created by Cover.

1. Basic assertions

This is a simple example of Spring Service source code with a trivial getter. Cover can write a Spring Boot test for the getter in a service provider by inlining Arrange, Act, and Assert.

1.1 Source

import org.springframework.stereotype.Service;

public class SimpleService
  public String getValue() {
    return "a really simple service";

1.2 Test generated by Diffblue Cover

public class SimpleServiceDiffblueTest {
  private SimpleService simpleService;
  public void testGetValue() {
    // Arrange, Act and Assert
    assertEquals("a really simple service", this.simpleService.getValue());

2. Mocking

This is a class that contains two methods to upload and download a file to/from Amazon S3 bucket. As the Amazon S3 bucket is a cloud storage mechanism, the test for this class/methods requires dependency injection. Cover can mock these types of dependencies and tests downloadFileFromBucket method by asserting the expected S3Object and the downloaded S3Object using the method.

2.1 Source

public class AmazonService {

  private AmazonS3 s3client;

  public PutObjectResult uploadFileToBucket(String bucketName, String key, File file) {
    return s3client.putObject(bucketName, key, file);

  public S3Object downloadFileFromBucket(String bucketName, String key) {
    return s3client.getObject(bucketName, key);

2.2 Tests generated by Diffblue Cover

public class AmazonServiceDiffblueTest {
  private AmazonS3Client amazonS3Client;
  private AmazonService amazonService;
  public void testUploadFileToBucket() {
    // Arrange
    PutObjectResult putObjectResult = new PutObjectResult();
    when(this.amazonS3Client.putObject(or(isA(String.class), isNull()), or(isA(String.class), isNull()),
        or(isA(File.class), isNull()))).thenReturn(putObjectResult);

    // Act and Assert
    assertSame(putObjectResult, this.amazonService.uploadFileToBucket("foo", "foo",
        Paths.get(System.getProperty("java.io.tmpdir"), "test.txt").toFile()));
  public void testDownloadFileFromBucket() throws UnsupportedEncodingException {
    // Arrange
    StringInputStream objectContent = new StringInputStream("file-name");
    S3Object s3Object = new S3Object();
    when(this.amazonS3Client.getObject(or(isA(String.class), isNull()), or(isA(String.class), isNull())))

    // Act and Assert
    assertSame(s3Object, this.amazonService.downloadFileFromBucket("foo", "foo"));

3. OS agnostic assertions

This example returns time in a different format. Cover writes a test to assert date and time that is not dependent on operating system or local time zones.

3.1 Source

public class TimeInAliceWonderland {

  private static final String DODO_DATETIME_FORMAT = "ss:mm:HH dd-MMM-yyyy";

  public static String reformatDodoDateTime(Date humanDate) {
    Map<String, DateFormat> dateFormatMap = new HashMap<>();
    dateFormatMap.put(DODO_DATETIME_FORMAT, new SimpleDateFormat(DODO_DATETIME_FORMAT));
    return dateFormatMap.get(DODO_DATETIME_FORMAT).format(humanDate);

3.2 Test generated by Diffblue Cover

  public void testReformatDodoDateTime() {
    LocalDateTime atStartOfDayResult = LocalDate.of(1970, 1, 1).atStartOfDay();
    assertEquals("00:00:00 01-Jan-1970", TimeInAliceWonderland

4. Example containing logic

In this example, Cover writes two tests to cover each pathway through the conditional - a test which adds 10 to the balance and asserts the new balance should be 20 and a second test where the account is closed.

4.1 Source

public class Account {
  private final long accountNumber;
  private final Client client;
  private long currentBalance;
  private String accountName;
  private AccountState accountState;

  public Account(final long accountNumber, final Client client, final long amount) {
    this.accountNumber = accountNumber;
    this.client = client;
    currentBalance = amount;
    accountName = "Current";
    accountState = AccountState.OPEN;

  public long getCurrentBalance() {
    return currentBalance;

  public void addToBalance(final long amount) throws AccountException {
    if (getAccountState() != AccountState.OPEN) {
      throw new AccountException("Cannot add to balance, account is closed.");
    currentBalance += amount;

4.2 Test generated by Diffblue Cover

  public void testAddToBalance() throws AccountException {
    // Arrange
    Account account = new Account(1234567890L, new Client("Carlos Welch"), 10L);

    // Act

    // Assert
    assertEquals(20L, account.getCurrentBalance());

5. Example of complex logic

This example code has 3 branches - Cover covers 100% of branches and creates three tests for three cases using existing enum values.

5.1 Source

public class VirtualSnackCupboard {

  public static Snack whatCanISnackNow(int oClock) {
    if (oClock == 10) {
      return Snack.CHOCOLATE;
    } else if (oClock == 15) {
      return Snack.CAKES;
    } else {
      return Snack.VEGGIE;

5.2 Tests generated by Diffblue Cover

  public void testPickSnack() {
    assertEquals(VirtualSnackCupboard.Snack.CHOCOLATE, VirtualSnackCupboard.whatCanISnackNow(10));
    assertEquals(VirtualSnackCupboard.Snack.CAKES, VirtualSnackCupboard.whatCanISnackNow(15));
    assertEquals(VirtualSnackCupboard.Snack.VEGGIE, VirtualSnackCupboard.whatCanISnackNow(0));

As can be seen, Diffblue Cover can generate tests for a wide variety of code scenarios - we encourage you to try out Cover CLI and the Diffblue Cover plugin for IntelliJ to learn more.