Sunday, May 6, 2012

Using JUnit to Test LIbraries in Talend

Lately, I've been working on developing a data cleansing library in Talend. I developed the library using Talend User Routines then added the calls to the library in my components. It works real well so far. In fact, you can write tests for the library function using JUnit. I found Bekwam Data as a Service's post  to be very helpful in setting JUnit up. Basically, you have to make sure you download the latest JUnit jar and import it in you Talend. Simple.


Once you have that set, you'll notice that if the test fails the JUnit function throws an error like such

Exception in thread "main" java.lang.Error: java.lang.Error: java.lang.NoClassDefFoundError: org/junit/ComparisonFailure
at data_load.sandbox_0_1.Sandbox.tFileInputDelimited_6Process(Sandbox.java:768)
at data_load.sandbox_0_1.Sandbox.runJobInTOS(Sandbox.java:1659)
at data_load.sandbox_0_1.Sandbox.main(Sandbox.java:1444)
Caused by: java.lang.Error: java.lang.NoClassDefFoundError: org/junit/ComparisonFailure
at data_load.sandbox_0_1.Sandbox.tRowGenerator_10Process(Sandbox.java:1311)
at data_load.sandbox_0_1.Sandbox.tFileInputDelimited_6Process(Sandbox.java:759)
... 2 more
Caused by: java.lang.NoClassDefFoundError: org/junit/ComparisonFailure
at data_load.sandbox_0_1.Sandbox.tRowGenerator_10Process(Sandbox.java:1176)
... 3 more
Caused by: java.lang.ClassNotFoundException: org.junit.ComparisonFailure
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)



It is a clunky error message for something trivial such as a test failure. You can catch the message and print out the relevant error: 



        try{  
              assertEquals("Name of Test Case:","EXPECTED TEXT",Cleaning.cleanCompany("SOME TEST INPUT"));  
           } catch(org.junit.ComparisonFailure ex){  
                 System.out.println(ex.getMessage());  
           }  


Junit puts the non-matching substring within square brackets. 






The one annoying thing with this approach is that you can't get the number of all failing test cases as the error gets caught even if a single test case fails. Talend stops execution immediately and you see the JUnit message.


I'm also debating on the idea of moving the library development entirely in eclipse so I can use JUnit plugins to test my library.

No comments:

Post a Comment