7.1 @Suite annotation

And a few more frequently used annotations. Even if you don't use them, you will definitely see tests with them. Therefore, it is necessary to understand at least in general terms what is written there.

Tests can be combined into groups. There is a special annotation for this @Suite. Example:

@Suite
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages("com.codegym.test")
@IncludeClassNamePatterns(".*Tests")
class SuiteDemo {
}

In this case, the @Suite annotation is paired with other annotations.

  • SuiteDisplayName– sets the name of the test group in the log;
  • SelectPackages- sets the list of packages where to look for test classes;
  • IncludeClassNamePatterns– sets the pattern of test class names.

Why such difficulties? Well imagine that all tests of the project are executed, let's say, 50 hours. And to execute them after each commit is very expensive. In such a case, it is useful to create some separate test scripts and set up testing in a much more flexible way.

7.2 @Order annotation

Another common annotation is @TestMethodOrder. It allows you to specify the order in which test methods are called in the test class. It can be very useful when you know that method calls affect each other, but in a certain order, everything should work as it should. Used quite often.

First, you can set the methods to be called in alphabetical order :

@TestMethodOrder(MethodOrderer.MethodName.class)
public class AlphanumericOrderUnitTest {

}

Secondly, each method can have a special annotation with its ordinal number.

@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {

    @Test
    @Order(1)
    public void firstTest() {
    }

    @Test
    @Order(2)
    public void secondTest() {
    }
}

Or even create a separate class that will specify the order in which the tests are called:

@TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest {

}

//sort method names alphabetically, but ignoring case
public class CustomOrder implements MethodOrderer {
    public void orderMethods(MethodOrdererContext context) {
        context.getMethodDescriptors().sort(
        (MethodDescriptor m1, MethodDescriptor m2)->
           m1.getMethod().getName().compareToIgnoreCase(m2.getMethod().getName()));
    }
}

7.3 @DisplayName annotation

Finally, each test can be given a name. It can be convenient if there are a lot of tests and you create special scenarios (subsets) of tests. There is a special annotation for this @DisplayName.

Example:

@DisplayName("Friendly name for the test")
public class DisplayNameCustomTest {

    @Test
    @DisplayName("Input Validation")
    void inputData() {
    }

    @DisplayName("Checking critical situations")
    @Test
    void criticalCases() {
    }
}

As in the case of setting the order of tests, here you can create a special method that will generate the names of tests and test methods. Example:

@DisplayNameGeneration(DisplayNameGeneratorUnitTest.ReplaceCamelCase.class)
class DisplayNameGeneratorUnitTest {

    @Test
    void camelCaseName() {
    }

    static class ReplaceCamelCase extends DisplayNameGenerator.Standard {
        @Override
        public String generateDisplayNameForClass(Class testClass) {
            return super.generateDisplayNameForClass(testClass).toUpperCase();
        }
 }
}

See how easy it is :)