Basic JUnit integration

We have already seen a simple example of a JUnit Serenity test shown earlier on First Steps:

@RunWith(SerenityRunner.class)      (1)
public class ASimpleTest {

    @Steps
    MathWizSteps michael;           (2)

    @Test
    public void addingSums() {
        // Given
        michael.startsWith(1);       (3)

        // When
        michael.adds(2);             (3)

        // Then
        michael.shouldHave(3);       (3)
    }
}
1 You run the JUnit test using the Serenity test runner
2 The @Steps annotation marks a Serenity step library
3 Create the test following the Given/When/Then pattern and using step methods from the step library

The most important thing here is the SerenityRunner test runner. This class instruments any step libraries in your class, and ensures that the test results will be recorded and reported on by the Serenity reporters.

Human-readable method titles

By default, Serenity will convert the test method names into a readable form in the reports. This will convert both camelCasedMethods and methods_with_underscores into a form with spaces. So both 'shouldCalculateCorrectOutcome()' and 'should_calculate_correct_outcome()' will appear as "Should calculate correct outcome" in the test reports.

You can override this convention by adding a @Title annotation onto the test method, as shown here:

@RunWith(SerenityRunner.class)
public class WhenEarningFrequentFlyerStatus {

    @Steps
    TravellerStatusSteps travellerSteps;

    @Test
    @Title("Members earn Gold status after 5000 points (50000 km)")         (1)
    public void earnGoldAfter5000Points() {
        // GIVEN
        travellerSteps.a_traveller_joins_the_frequent_flyer_program();

        // WHEN
        travellerSteps.the_traveller_flies(50000);

        // THEN
        travellerSteps.traveller_should_have_a_status_of(Gold);
    }
}
1 The @Title annotation lets you provide your own title for this test in the test reports

Also @Title can be used for providing information about issues for this test, you can find more info in Linking scenarios/tests with issues