Friday, 9 September 2011

Maven and Hibernate and Unit Tests (example using the JDK logger)

While attempting to run some unit tests that rely on the H2 in-memory database, I saw this failure:
...
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
	at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
	at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
	at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
	at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
	at org.hibernate.cfg.Configuration.(Configuration.java:165)
	[snip]
	... 74 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
	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)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
	... 82 more

The maven dependency:tree target shows:
[INFO] +- org.hibernate:hibernate-core:jar:3.5.0-Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.5.8:compile

The org.slf4j:slf4j-api.jar provides the interface, but not a logging implementation. To add the JDK logger implementation, and additional dependency is required:
<dependency>
	<groupid>org.slf4j</groupid>
	<artifactid>slf4j-jdk14</artifactid>
	<version>1.5.8</version>
	<scope>test</scope>
</dependency>

(version 1.5.8 is required to match the api dependency in the hibernate 3.5.0-Final libraries.

No comments:

Post a Comment