Testing the Plug-in | ||
---|---|---|
Previous | Next | |
Creating the Plug-in | Releasing 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.
To create a test project:
fragment
. The list shows the
Fragment Project wizard.
com.acme.phoneStandardizerTests
. Leave the other fields as is.
1.0.0
)
Phone Standardizer Test
)
ACME Corp.
)
com.acme.phoneStandardizer
.
com.acme.phoneStandardizer
org.junit4
. Select the
org.junit4
plug-in in the list of matching items and then click
OK.
To create a test case:
com.acme.phoneStandardizerTests
), right click and then select
File > New > Other.
junit
. The list shows the
JUnit Test Case wizard. Select it from the list and then click
Next.
com.acme.phonestandardizer.tests
TestPhoneFrance
.
com.acme.phonestandardizer.IntlPhoneStandardizer
transform
method for an enricher or the
isValid
method for a validation.
Note: In addition to the
transform
andisValid
methods, you may also test other methods such assetUp
andtearDown
to verify the plug-in behavior during its entire lifecycle.
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:
IntlPhoneStandardizer
object.
INPUTPHONE
input field value for this data row.
transform
method.
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);
IDataRow inDataRow = new DataRow();
((DataRow) inDataRow).setValue("INPUTPHONE", inputPhone);
List<IDataRow> inDataRowList = new ArrayList<IDataRow>();
inDataRowList.add(inDataRow);
List<IDataRow> outDataRowList = phoneStd.transform(inDataRowList);
assertEquals(expectedOutputPhone, outDataRowList.get(0).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.
To run the test case:
The test cases should display no error.
Previous | Top | Next |
Creating the Plug-in | Releasing the Plug-in |