Total Pageviews

Thursday, February 02, 2012

Don't use commons lang < 2.4 with HtmlUnit

In case you have a project which has a dependency to Apache commons-lang and you plan to use the latest HtmlUnit (as of writing this post this is HtmlUnit 2.9) you should take care of the version of commons-lang.

HtmlUnit is using a method (startsWithIgnoreCase) from commons-lang that was introduced in commons-lang 2.4.
So any commons-lang version below 2.4 will produce runtime exceptions when using HtmlUnit. The stacktrace may be similar to this one:

 java.lang.NoClassDefFoundError: Could not initialize class  
 com.gargoylesoftware.htmlunit.WebClient  
     at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.createWebClient(HtmlUnitTestingEngineImpl.java:804)  
     at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.initWebClient(HtmlUnitTestingEngineImpl.java:813)  
   
 also :  
   
 java.lang.NoSuchMethodError:  
 org.apache.commons.lang.StringUtils.startsWithIgnoreCase(Ljava/lang/String;Ljava/lang/String;)Z  
     at com.gargoylesoftware.htmlunit.util.URLCreator$URLCreatorStandard.toUrlUnsafeClassic(URLCreator.java:66)  
     at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlUnsafe(UrlUtils.java:193)  
     at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlSafe(UrlUtils.java:171)  
     at com.gargoylesoftware.htmlunit.WebClient.<clinit>(WebClient.java:162)  
     at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.createWebClient(HtmlUnitTestingEngineImpl.java:804)  
     at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.initWebClient(HtmlUnitTestingEngineImpl.java:813)  
   
   
   

In case you use Maven as your build tool you can check the dependencies using the command:

mvn dependency:tree

To avoid this error you can declare version 2.6 of commons-lang in your project.