Automating the creation of unit tests is on the wishlist of many developers, and there are only a few tools available today that generate unit tests for Java. Many of them will save developers time and effort, but the tests themselves can vary quite a lot depending on the tool you use—from boilerplate stubs all the way to fully functioning tests with the ability to even generate complex mocks.

Most unit test generators actually create boilerplate code for tests, rather than tests that compile and run. These tools guess the inputs that can be used as a starting point, but the developer has to finish them off to get functioning tests.They also don’t provide the assertions that are needed to run the test and validate the logic of the method.

These tests can still be useful; they save developers time in creating the test classes and build out the basic structure, and even help remind you what tests still need to be written. But it means the developer has to spend time and energy thinking about the goal of the test and filling in the content of each one. Diffblue Cover, on the other hand, is uniquely able to create complete human-readable unit tests that are ready to run immediately:

@SpringBootTest
public class AmazonServiceDiffblueTest {
  @MockBean
  private AmazonS3Client amazonS3Client;
  @Autowired
  private AmazonService amazonService;

  @Test
  public void testUploadFileToBucket() {
    // Arrange
    PutObjectResult putObjectResult = new PutObjectResult();
    putObjectResult.setContentMd5("file-hash");
    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()));
  }

  @Test
  public void testDownloadFileFromBucket() throws UnsupportedEncodingException {
    // Arrange
    StringInputStream objectContent = new StringInputStream("file-name");
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(objectContent);
    when(this.amazonS3Client.getObject(or(isA(String.class), isNull()), or(isA(String.class), isNull())))
        .thenReturn(s3Object);
    // Act and Assert
    assertSame(s3Object, this.amazonService.downloadFileFromBucket("foo", "foo"));
  }
}
Example of a Spring Boot test with a complex mock of an Amazon S3 bucket created in 600ms by Diffblue Cover.

AI for Code

Diffblue Cover is an automated unit test-writing tool. It analyzes your existing Java application and writes unit tests that reflect the current behavior, increasing test coverage and helping you find regressions in future code changes. It constructs tests with human-readable inputs that relate to the project and the world around them, calls the method under test, and (most importantly) asserts the result of that method. A test is only as good as its assertions, which Cover calculates, ranks, and implements during test creation.

This results in a fully formed test that can be run without human intervention. The tests Diffblue Cover creates aim to secure the logic of the method and protect the code against unintended changes in the future.

Other Advantages

Diffblue Cover’s test generation is fully automated, and hundreds of tests can be created in minutes for even the largest and most complex enterprise codebases. Because the tool explores code paths that a developer might not think of, Cover’s tests can catch edge cases that might have otherwise been missed.

To see the tests created by Diffblue Cover on your own code, sign up for a free trial.