Activity 18: Generate JUnit tests

To generate JUnit test cases for for the Java implementation of the vending machine, an adapter is needed between the test cases and the system under test. The test case generator for JUnit assumes a file Adapter.java is available in a package with the same name as the generated package that contains TestCases.java, i.e., “test.[name of interface]”. For interface IService it can be found at location src/test.IService.

For the vending machine, this file has already been written manually.

  • Extend project file VendingMachine.prj with the following task:

    Generate Test {
        Test_IService for interface IService {
            max-depth: 1000
            params: 'IService/IService.params'
            template: java
            output-file: 'TestCases.java'
        }
    }
  • Execute the CommaSuite workflow on VendingMachine.prj and inspect the resulting file TestCases.java in folder src-gen/testcases/IService.

  • Ensure JUnit 5 is available (this has already been done for the vending machine):

    Right-click on project, select Build Path > Configure Build Path;
    select tab Libraries > Add Library > select JUnit5
  • Define src-gen as source folder:

    Right-click on project, select Build Path > Configure Build Path;
    select tab Source > Add Folder > select src-gen
  • Since the adapter also starts logging the tests as .events files, create a folder EventFilesTests to store these traces (right click on the vending machine project, select New > Folder).

  • Run the test cases, by right clicking on TestCases.java and selecting Run As > JUnit Test.

    • Note that the .events files in EventFilesTests might be checked by monitoring as a kind of consistency check.