Exercise 3: Create Your Test Data With a Factory Method

This exercise helps you to understand how you can create the test data that’s passed to a parameterized test by using a factory method. Let’s start by taking a quick look at the system under test.

The ProgrammingLanguage enum specifies different programming languages. Its source code looks as follows:

public enum ProgrammingLanguage {

    C,
    CLOJURE,
    SMALLTALK
}

The ProgrammingParadigm enum specifies different programming paradigms. It also has one static method called fromProgrammingLanguage(). This method returns the programming paradigm that’s used by the programming language given as a method parameter. The source code of the ProgrammingParadigm enum looks as follows:

public enum ProgrammingParadigm {

    FUNCTIONAL,
    OBJECT_ORIENTED,
    PROCEDURAL;

    public static ProgrammingParadigm fromProgrammingLanguage(
            ProgrammingLanguage source
    ) {
        return switch(source) {
            case C -> ProgrammingParadigm.PROCEDURAL;
            case CLOJURE -> ProgrammingParadigm.FUNCTIONAL;
            case SMALLTALK -> ProgrammingParadigm.OBJECT_ORIENTED;
        };
    }
}

During this exercise you will create the test data that’s passed to a parameterized test method which ensures that the fromProgrammingLanguage() method of the ProgrammingParadigm enum is working as expected. You can finish this exercise by following these steps:

1. Open the ProgrammingParadigmTest class that’s found from the com.cleantestautomation.junit5intro package.

2. Create the factory method that creates the test data which is passed to the shouldReturnCorrectProgrammingParadigm() method. Remember that your test data must test every possible scenario.

3. Ensure that JUnit 5 creates the test data by using your factory method when it runs your parameterized test method.

4. Run your parameterized test and make sure that all test invocations pass.