Testing the Plug-in

It is recommended to implement tests to validate the plug-in behavior before deploying it to a Semarchy instance.
In this section, we will explain how to create a JUnit test case for the enricher plug-in created in the previous chapter.

Creating a Test Project

To create a test project:

  1. Select File > New > Other
  2. In the Wizards filter, enter fragment. The list shows the Fragment Project wizard.
  3. Select the Fragment Project wizard and then click Next.
  4. In the Project Name, enter the name for your project. For example: com.acme.phoneStandardizerTests. Leave the other fields as is.
  5. Click Next. In the Content page, modify the following:
  6. In the Host Plug-in section, use the Browse... button to select the Plug-in ID.
  7. In the Select a Plug-in filter, enter the name of the Semarchy plug-in that you want to validate with your tests. For example com.acme.phoneStandardizer.
  8. Select the Semarchy plug-in from the list. For example, select com.acme.phoneStandardizer
  9. Click Finish. A new project is created.
  10. In the project editor, select the Dependencies Tab.
  11. In the Required Plug-ins section, click the Add... button.
  12. In the Select a Plug-in filter, enter org.junit4. Select the org.junit4 plug-in in the list of matching items and then click OK.
  13. Press CTRL+S to save the editor.

Creating a Test Case

To create a test case:

  1. In the Package Explorer, select the test project ( com.acme.phoneStandardizerTests), right click and then select File > New > Other.
  2. In the Wizards filter, enter junit. The list shows the JUnit Test Case wizard. Select it from the list and then click Next.
  3. In the JUnit Test Case wizard, enter the following:
  4. Click Next. Select the methods that you want to test. Typically, the transformOneRow method for an enricher or the validateOneRow method for a validation.
  5. Click Finish. The code editor for your test case opens.

Note: In addition to the transformOneRow and validateOneRow methods, you may also test other methods such as setUp and tearDown to verify the plug-in behavior during its entire lifecycle.

Implementing the Test Case

The test case checks the transformation of several phone numbers. It checks that the transformation provides expected results with valid, invalid or null phone numbers.

Tip: The full code of the TestPhoneFrance.java class is provided in Appendix B. You can copy and paste the code from the appendix in the class editor.

The rest of this section describes the key methods implemented in the test code.

The checkPhoneTransform method tests the behavior of the transformer. It takes three parameters:

The method:

  1. Instantiates an IntlPhoneStandardizer object.
  2. Creates the input DataRow and sets the INPUTPHONE input field value.
  3. Runs the transformOneRow method.
  4. Compares (using the JUnit assertEquals method) the value of the STANDARDIZEDPHONE field of the output IDataRow with the expected standardized phone.
	private void checkPhoneTransform(Boolean nullifyOnError, String inputPhone,
			String expectedOutputPhone) {
		IntlPhoneStandardizer phoneStd = new IntlPhoneStandardizer();
		phoneStd.setNullify(nullifyOnError);
		DataRow indata = new DataRow();
		indata.setValue("INPUTPHONE", inputPhone);
		IDataRow outdata = phoneStd.transformOneRow(indata);
		assertEquals(expectedOutputPhone, outdata.getValue("STANDARDIZEDPHONE"));
	}

This method is used in various test cases, as shown below.

  @Test
  public void testTransformNullPhones() {
    checkPhoneTransform(true, null, null);
    checkPhoneTransform(false, null, null);
  }

  @Test
  public void testTransformBadPhones() {
    checkPhoneTransform(true, "abcd", null);
    checkPhoneTransform(false, "abcd", "abcd");
    checkPhoneTransform(true, "64169710", null);
    checkPhoneTransform(false, "64169710", "64169710");
    checkPhoneTransform(true, "111164169710", null);
    checkPhoneTransform(false, "111164169710", "111164169710");
    checkPhoneTransform(true, "+44664169710", null);
    checkPhoneTransform(false, "+44664169710", "+44664169710");
    checkPhoneTransform(true, "1664169710", null);
    checkPhoneTransform(false, "1664169710", "1664169710");
  }

Note: The unit tests only include French phone numbers, as it is what the transformer supports. Valid French Phone numbers have 10 digits, starting with a zero. They may be written with non-numeric characters that will be ignored. They may be prefixed with the “+33” country code.

Running the Test Case

To run the test case:

  1. Save the test case code.
  2. Select the test project, right-click and select Run As > JUnit Test. The JUnit view opens and show the execution results for the test case.

The test cases should display no error.