Testing the rules by using JUnit

Once a test rule has been created, it can be analyzed as part of a JUnit test to confirm that the rule meets all criteria for execution. The WindupRulesMultipleTests class in the {ProductShortName} rules repository is designed to test multiple rules simultaneously, and provides feedback on any missing requirements.

Prerequisites
Creating the JUnit test configuration

The following instructions detail creating a JUnit test using Eclipse. When using a different IDE, it is recommended to consult your IDE’s documentation for instructions on creating a JUnit test.

  1. Import the {ProductShortName} rulesets repository into your IDE.

  2. Copy the custom rules, along with the corresponding tests and data, into </path/to/RULESETS_REPO>/rules-reviewed/<RULE_NAME>/. This should create the following directory structure.

    Directory structure
    ├── *rules-reviewed/*  _(Root directory of the rules found within the project)_
    │   ├── *<RULE_NAME>/*  _(Directory to contain the newly developed rule and tests)_
    │   │   ├── *<RULE_NAME>.windup.xml*  _(Custom rule)_
    │   │   ├── *tests/*  _(Directory that contains any test rules and data)_
    │   │   │   ├── *<RULE_NAME>.windup.test.xml* _(Test rule)_
    │   │   │   └── *data/*  _(Optional directory to contain test rule data)_
  3. Select Run from the top menu bar.

  4. Select Run Configurations…​ from the drop down that appears.

  5. Right-click JUnit from the options on the left side and select New.

  6. Enter the following:

    • Name: A name for your JUnit test, such as WindupRulesMultipleTests.

    • Project: Ensure this is set to windup-rulesets.

    • Test class: Set this to org.jboss.windup.rules.tests.WindupRulesMultipleTests.

      junit test
  7. Select the Arguments tab, and add the -DrunTestsMatching=<RULE_NAME> VM argument. For instance, if your rule name was community-rules, then you would add -DrunTestsMatching=community-rules as seen in the following image.

    junit test arguments
  8. Click Run in the bottom right corner to begin the test.

    When the execution completes, the results are available for analysis. If all tests passed, then the test rule is correctly formatted. If all tests did not pass, it is recommended to address each of the issues raised in the test failures.