Author: Peter Schrammel Almost nobody likes writing unit tests - and if you agree, you may be glad to hear that manually writing all of your tests is becoming a thing of the past. We've developed , an AI tool that automatically creates unit tests for Java code, to make it easier to get the unit tests you need without spending the time it takes to write them. Diffblue Cover Diffblue Cover: is a free IntelliJ plugin that uses the same core technology, and is now available for anyone to use with their open-source Java projects. What does Community Edition do? So how is it actually used? Community Edition Diffblue Cover works like this: First, Diffblue Cover ensures that your code is compiled. This is required because it performs an analysis on the bytecode. Then, it tries to trigger executions of paths through each method that it is going to test, using inputs that are similar to those a developer would choose. On the way, it mocks any dependencies that should be mocked. Finally, it figures out what the useful assertions may be and adds them to the test. Note that the assertions always reflect the current behavior of the code; Diffblue Cover is not able to anticipate your intentions and 'fix' the code for you. Diffblue Cover is intended to be used for unit regression testing. Essentially, this means it creates unit tests that reflect the current behavior of your code (and if you'd like more information about that, we have an explanation here). If your code is currently working, Diffblue Cover captures this. Later, when you make changes to the code, these tests may fail and show you possibly unintended changes to the code (so-called "regressions"). It can also help you find bugs by review. What this means for developers Diffblue Cover is significantly faster than writing manual tests from scratch. Sometimes it might not succeed in finding a suitable test for your code, in which case it may return a partial test for you to complete. But because Diffblue Cover creates unit tests in bulk in , it helps you speed up unit test writing for new features. a tiny fraction of the time it'd take to write them Currently, Diffblue Cover is only available for Java, but will be expanded to work with other languages in the future. Generated unit tests that look like they were written by a person The tests that Diffblue Cover creates largely follow the arrange-act-assert pattern: Arrange inputs and mocks Act: run method under test Assert effects Here's an example that Cover created: @MockBean private AmazonS3Client amazonS3Client; @Test public testDownloadFileFromBucket() throws UnsupportedEncodingException { StringInputStream objectContent = StringInputStream( ); S3Object s3Object = S3Object(); s3Object.setObjectContent(objectContent); when( .amazonS3Client .getObject(or(isA( .class), isNull()), or(isA( .class), isNull()))) .thenReturn(s3Object); S3Object actualS3Object = .cloudStorageService.downloadFileFromBucket( , ) assertSame(s3Object, actualS3Object); } void new "Lorem ipsum dolor sit amet." new this String String this "bucket-name" "key" Installation and first steps You can download the plugin , or by going to Preferences > Plugins > Marketplace in your IntelliJ IDE, searching for “Diffblue Cover,” and installing the community edition. here To give you an idea of what the tool does and illustrate its features, let’s look at a simple example project that uses the Spring framework: Spring Pet-Clinic. We have forked this project into . You’ll find the branches for each lesson of this tutorial so that you can follow along on your own. https://github.com/Diffblue-benchmarks/Spring-petclinic Your first steps after having installed the plugin are: git clone https://github.com/Diffblue-benchmarks/Spring-petclinic cd Spring-petclinic git checkout a-test-intro Open project in IntelliJ Navigate to the CloudStorageService class This class implements a simple service for uploading to and downloading files from an S3 bucket. @Service public { @Autowired private AmazonS3 s3client; public PutObjectResult uploadFileToBucket( bucketName, key, File file) { s3client.putObject(bucketName, key, file); } public S3Object downloadFileFromBucket( bucketName, key) { s3client.getObject(bucketName, key); } class CloudStorageService String String return String String return To create tests for this class, right-click on CloudStorageService > Write tests When you open the event log, you'll see what the tool is doing. First, it ensures that the project compiles and figures out for which methods we want to produce unit tests. To create the tests, it has to load the Spring context and find suitable inputs to the methods to exercise paths through these methods. Once the first test has been created you can navigate to the created test source file to see the tests. Further tests will drop in as they are produced. Let’s have a look at this test. The tested service would communicate with an actual S3 bucket. We don’t want this to happen in a unit test, so we mock the S3 client using Mockito. We have to set up the object returned by getObject appropriately, including the content of the file. @ExtendWith(SpringExtension.class) public { @MockBean private AmazonS3Client amazonS3Client; @Autowired private CloudStorageService cloudStorageService; @Test public testDownloadFileFromBucket() throws UnsupportedEncodingException { StringInputStream objectContent = StringInputStream( ); S3Object s3Object = S3Object(); s3Object.setObjectContent(objectContent); when( .amazonS3Client .getObject(or(isA( .class), isNull()), or(isA( .class), isNull()))) .thenReturn(s3Object); assertSame(s3Object, .cloudStorageService.downloadFileFromBucket( , )); } class CloudStorageServiceTest void new "Lorem ipsum dolor sit amet." new this String String this "bucket-name" "key" In this example, you can see how Diffblue Cover: Community Edition creates tests that are actually useful and can even handle more complex scenarios, e.g. mocking. Hopefully this overview gives you an idea of how Community Edition works, but if you have questions or need any help, you can contact us through the . Community Forum