Test Naming

Test classes and methods created by Diffblue Cover are named after the class and methods under test using configurable templates. To change the naming, go to Diffblue > Change Settings in IntelliJ and update the Test Naming section as needed.

Class Name Template

Description: Used to define the test class naming convention for tests written by Diffblue Cover. The ${CLASS} variable will be substituted with the name of the class under test.

Default: ${CLASS}DiffblueTest

Example: ${CLASS}CreatedTest

public class UserAccessCreatedTest {

    public String currentUser;

    ...
}

Method Name Template

Description: Used to define the test method naming convention for tests written by Diffblue Cover. The following variables can be used:

  • ${INNER} - substituted with the name of the inner class for the method under test. If there's no inner class this will be an empty string.

  • ${UNIT} - usually substituted with the name of the method under test. Where the unit under test comprises multiple methods (getters and setters, equals and hash code) the more general unit under test name is used.

  • ${METHOD} - substituted with the name of the first method under test, typically the only method under test. To avoid duplication, do not use ${UNIT} and ${METHOD} together.

  • ${GIVEN} - substituted with a summary of the conditions before testing, or else blank.

  • ${WHEN} - substituted with a summary of the conditions under test, or else blank.

  • ${THEN} - substituted with a summary of the test's consequences, or else blank.

  • ${_} - substituted with an underscore, or blank if there are no values to separate.

Default: test${INNER}${UNIT}${_}${GIVEN}${_}${WHEN}${_}${THEN}

Example: aitest${INNER}${UNIT}

@Test
public void aitestGettersAndSetters() {
    // Arrange
    ComponentPojo componentPojo = new ComponentPojo();

    // Act
    componentPojo.setName("Name");
    componentPojo.setSize(3);
    String actualName = componentPojo.getName();
    // Assert that nothing has changed
    assertEquals("Name", actualName);
    assertEquals(3, componentPojo.getSize());
}

@Test
public void aitestEqualsAndHashCode() {
    // Arrange
    DoubleArgumentType doubleArgResult = DoubleArgumentType.doubleArg(10.0d, 10.0d);

    // Act and Assert
    assertEquals(doubleArgResult, doubleArgResult);
    int expectedHashCodeResult = doubleArgResult.hashCode();
    assertEquals(expectedHashCodeResult, doubleArgResult.hashCode());
}

Descriptive Test Names

Descriptive ${GIVEN} , ${WHEN} and ${THEN} summaries are populated when disambiguating multiple tests for the same method under test, and are added to the test method name up to a maximum of 80 characters. When descriptive test names are enabled they are additionally used to provide improved javadoc comments, and to provider clearer test display names when the test framework supports it.

/**
 * Test {@link BaseEntity#isNew()}; given BaseEntity Id one; then returns false.
 * <p>
 * Method under test: {@link BaseEntity#isNew()}
 */
@Test
@DisplayName("Test isNew(); given BaseEntity Id one; then returns false")
void testIsNew_givenBaseEntityIdOne_thenReturnsFalse() {
	// Arrange
	BaseEntity baseEntity = new BaseEntity();
	baseEntity.setId(1);

	// Act and Assert
	assertFalse(baseEntity.isNew());
}

/**
 * Test {@link BaseEntity#isNew()}; given BaseEntity; then returns true.
 * <p>
 * Method under test: {@link BaseEntity#isNew()}
 */
@Test
@DisplayName("Test isNew(); given BaseEntity; then returns true")
void testIsNew_givenBaseEntity_thenReturnsTrue() {
	// Arrange, Act and Assert
	assertTrue((new BaseEntity()).isNew());
}

When ${UNIT} is describes a single method under test that has overloads then the method under test's arguments are additionally described.

/**
 * Test {@link Owner#getPet(String)} with name.
 * <p>
 * Method under test: {@link Owner#getPet(String)}
 */
@Test
@DisplayName("Test getPet(String) with name")
void testGetPetWithName() {
	// Arrange, Act and Assert
	assertNull((new Owner()).getPet("Bella"));
}

/**
 * Test {@link Owner#getPet(String, boolean)} with name, ignoreNew.
 * <p>
 * Method under test: {@link Owner#getPet(String, boolean)}
 */
@Test
@DisplayName("Test getPet(String,boolean) with name, ignoreNew")
void testGetPetWithNameIgnoreNew() {
	// Arrange, Act and Assert
	assertNull((new Owner()).getPet("Bella", true));
}

Descriptive test names are enabled by default but can be disabled via the "Descriptive Test Names" setting.

Numeric Disambiguation

Disambiguation between tests of the same name is performed automatically using sequential numbering:

@Test
public void testAppendTitle() {
    ...
}

@Test
public void testAppendTitle2() {
    ...
}

Last updated

Was this helpful?