Maven, JMock and Hibernate

I use JMock a lot. I use Hibernate a lot. And now I’m using Maven more and more. Those three things aren’t terribly fond of living together comfortably.

Here’s the problem. If you want to mock abstract classes in addition to interfaces, then JMock depends on CGLIB. Hibernate also depends on CGLIB. Unfortunately, the Maven POMs for those two packages introduce a situation where Maven can’t recognize that it’s being asked to use two very different releases of CGLIB. Hibernate depends on CGLIB 2.1 (which gets resolved to a cglib-2.1_3.jar), and JMock depends on version 2.0. Unfortunately, the artifact id for the former is “cglib” and the artifact id for the latter is “cglib-full”. Maven can’t recognize that those two artifacts overlap. And so I get two different versions of CGLIB on my classpath, and whenever I run Hibernate unit tests, I get a nasty Exception:

java.lang.NoSuchMethodError: net.sf.cglib.proxy.Enhancer.setInterceptDuringConstruction(Z)V

It’s a simple thing to resolve… just use an exclusion (like so):

<dependency>
  <groupId>jmock</groupId>
  <artifactId>jmock-cglib</artifactId>
  <version>1.1.0</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>cglib</groupId>
      <artifactId>cglib-full</artifactId>
    </exclusion>
  </exclusions>
</dependency>

I’ve used CGLIB, directly, on past projects, and I understand what it’s doing pretty well. So it was easy for me to figure out how to resolve the problem. But i can easily imagine a lot of people wasting a lot of time trying to figure out what’s happening.

Print Friendly
This entry was posted in Technology by BC Holmes. Bookmark the permalink.

About BC Holmes

BC Holmes is an architect with 20+ years experience designing and building applications, currently specializing in eHealth and mobile applications. She has worked in the IT Services industry and the financial services industry. She holds a joint honours in Pure Mathematics and Theatre Arts. BC has presented papers at JavaOne ("Developing an Internet Architecture for the Enterprise", "Fast Feedback Loop: JUnit Test Strategies for Tapestry"), XP Toronto ("Good Design: Know It When I See It?") and the Toronto WebSphere Users' Group ("Extreme WebSphere").

Leave a Reply