Techniques for Unit Testing

From wiki.searchtechnologies.com
Jump to: navigation, search

For Information on Aspire 3.1 Click Here

Technique #1: Testing A Pipeline Stage In Isolation

Generally, it should be possible to test a component all by itself. To do this, all you need to do is create a new Job, then Job Object (an AspireObject), and then fill out all of the members of the Job and the Job Object which are used by your stage. Then, send the job to your stage with the process() method and inspect the job after your stage is done with it.

Use JobFactory.newInstance() to create a job. See Aspire Job Class for more information.

This is the easiest method for testing a pipeline stage. This is also the method set up automatically by the aspire-stage-archetype Maven Archetype.

NOTE: The examples in this section are just examples. Replace the data (i.e. the configurations, test strings, etc.) with the needs for your own stage.

1. Initialize the Unit Test Helper.

The Unit Test Helper provides methods which help simplify unit testing, including creating directories and files for test output, automatically comparing files to "known good" versions, and scanning strings for regular expressions, plus a number of other useful features.

   UnitTestHelper uth = new UnitTestHelper(this.getClass());

2. Get an instance of your new stage

   ExtractTextStage myStage = new ExtractTextStage();

3. Initialize your stage with initialize():

   myStage.initialize(AXML.stringToDom(
       "<config>" +
         "<tikaConfig>src/main/resources/tika-config.xml</tikaConfig>" +
         "<metadataMap>" +
           "<map from=\"DC.title\" to=\"myDublinCoreTitle\"/>" +
           "<map from=\"DC.description\" to=\"myDublinCoreDescription\"/>" +
         "</metadataMap>" +
       "</config>"));

Note: AXML is an Aspire utility class for easily creating and manipulating XML. It wraps the W3C XML DOM object.

4. Initialize an AspireObject:

    AspireObject doc = new AspireObject(new StringReader(
        "<doc><fetchUrl>http://dublincore.org/</fetchUrl></doc>"));

5. Create a job using the document as the object:

   Job j = JobFactory.newInstance(doc);

6. Process the job with your stage:

    myStage.process(j);

7. Check that the necessary changes to the AspireDocument have been made:

   System.out.println(doc.toString(true));
   assertTrue(uth.scanStringForRegex(doc.toString(true), "The Dublin Core Metadata Initiative"));

Technique #2: Testing Within an Instance of Aspire

The second method for unit testing is to test your pipeline stage within an entire instance of Aspire, embedded inside your unit test.

This uses the ComponentTestBench, available from the aspire-ctb artifact.

(more to be written later)