Posts mit dem Label JUnit werden angezeigt. Alle Posts anzeigen
Posts mit dem Label JUnit werden angezeigt. Alle Posts anzeigen

Montag, 11. Juni 2012

Testing UIMA with JUnit - I

Some basic functionc can help to test UIMA functions with JUnit tests. Here is my used UIMATestUtils.class:

public class UIMATestUtils {
   
    /**
     * Read type system
     * @return
     * @throws InvalidXMLException
     * @throws IOException
     */
    static public TypeSystemDescription readTypeSystem() throws InvalidXMLException, IOException {
        URL myURL = UIMAFramework.class.getResource("/TypeSystem.xml");
        TypeSystemDescription typeSysDes = UIMAFramework.getXMLParser()
                .parseTypeSystemDescription(new XMLInputSource(myURL));
        return typeSysDes;
    }

    /**
     * Create CAS
     * @param inputFolder
     * @return
     * @throws IOException
     * @throws InvalidXMLException
     * @throws ResourceInitializationException
     * @throws CollectionException
     */
    static public CAS createCas(String inputFolder) throws ResourceInitializationException, InvalidXMLException, IOException, CollectionException {
        CollectionReaderDescription crDesc = CollectionReaderFactory
                .createDescription(XCasReader.class, readTypeSystem(),
                        AbstractDeployer.PARAM_INPUTDIR, inputFolder);
        XCasReader reader = (XCasReader) UIMAFramework.produceCollectionReader(crDesc);
        CAS cas = CasCreationUtils.createCas(reader.getProcessingResourceMetaData());
        reader.run(cas);
        return cas;
    }

    /**
     * Create JCas
     * @param inputFolder
     * @return
     * @throws IOException
     * @throws InvalidXMLException
     * @throws ResourceInitializationException
     * @throws CASException
     * @throws CollectionException
     */
    static public JCas createJCas(String inputFolder) throws ResourceInitializationException, InvalidXMLException, IOException, CASException, CollectionException {
        CAS cas = createCas(inputFolder);
        return cas.getJCas();
    }
The XCasReader thereby is an XMIDeserializer, which mainly calls the UIMA function XmiCasDeserializer.deserialize(in, aCAS);.

To test UIMA functions, you often need the typesystem descriptor, JCas, or CAS objects as parameters. These functions handle this for you, whereby the JCas and CAS creation is based on the xmi deserializer function of UIMA. This requires a folder containing the xmi file you want to use as base for you JCas or CAS object. 

Mittwoch, 23. Mai 2012

JUnit - Testing for excepted exceptions

Testing for excepted exceptions is a commen use case. In JUnit 4 there is a nice possibilities to do this:
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void throwsNullPointerExceptionWithMessage() {
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("Exception Message"); 
    // test code goes here
}
The rule just has to be defined once, and then can be reused wherever wanted. The expectMessage function just makes a substring search, so you do not need to have to provide the complete error message.

In JUnit < 4, the testing is more complex:
@Test
public void throwsNullPointerExceptionWithMessage() {
    boolean exceptionThrown = false;
    try {
        // test code goes here
    } catch (Exception e) {
        exceptionThrown = true;
        Assert.assertEquals("Not expected exection type", NullPointerException.class, e.getClass());
        Assert.assertTrue("Not expected error message", e.getMessage().contains("Exception Message")); 
    }
    Assert.assertTrue("Expected exception not thrown", exceptionThrown);
}