Skip to content

UnitTestingConventions

Cédric Mayer edited this page Sep 4, 2017 · 8 revisions

Naming Conventions for Test Methods

Class Names

All test classes which should be executed need a Test suffix. If a unit test targets a specific class, the name is:

<SutClass>Test

If there is no specific class (for example in case of component/integration tests) the name should be

<Topic>Test

If tests have common base classes which should not be executed on its own, these abstract base classes are named *TestBase.

Method Names

The method names follow this pattern:

<sut>_should_<expectedBehavior>_when_<State>
Part Description
sut Name of the method which is tested if a specific method is tested. For constructors the name is init. If no specific method is tested (for example integration tests) this part can be omitted.
expectedBehaviour Description of the expected behavior.
state If the test is executed on a specific pre-condition, this should be named here. If the test does not require a specific state, this part can be omitted.

Examples:

@Test
public void hashCode_should_return_same_value_when_all_attributes_are_identical()

@Test
public void visitField_should_throw_IllegalStateException_when_JaCoCo_instrumentation_field_is_present()

@Test
public void should_insert_frames_when_class_file_with_version_16_is_provided()

Rules

  1. One Aspect per Test Case: Each test case should cover only one aspect. This allows precise documentation (see naming conventions). In case of test failures we have a list of all broken aspects.