Assertions suggestions - Get passing assertion for your own tests

As a huge time saver, Diffblue Cover can suggest assertions for JUnit tests.

Here’s an example of a test that we started writing for the Client.takeFromBalance() method in: https://github.com/Diffblue-benchmarks/RicardoDiffblue-CoreBanking

package com.diffblue.corebanking.account;

import com.diffblue.corebanking.client.Client;
import org.junit.Assert;
import org.junit.Test;


public class AccountTest {
 @Test
 public void testTakeFromBalance() throws Exception {
   // Arrange
   Client client = new Client("John Smith");
   Account account = new Account(19928294, client, 10000);

   // Act
   account.takeFromBalance(250);

   // Assert

 }
}

We have setup the call to the method to test, but don’t yet have any assertions. Let Diffblue Cover write them for us by placing the cursor where we want them inserted and hit Alt+Shift+Space. After a couple of seconds we get this pop-up:

We can choose an assertion to insert (or several by using Ctrl+Click), and add it to our test. Let’s insert the one that checks the current balance. Our assertion section now looks like this:

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

The test will pass as Diffblue Cover gives us correct assertions, so we’re done and can move on to other things.

Some cases where Diffblue Cover may not be able to provide any assertion suggestions:

  • Usage of another framework than JUnit4
  • Test methods containing mocking
  • Test methods that do not already successfully run.

results matching ""

    No results matching ""