tags:

views:

343

answers:

3

I am trying to use EMMA to measure coverage of some JUnit tests that use JMockit. But when I try to run the JMockit tests after instrumenting with EMMA, about a quarter of the tests fail with the following error:

com.logstorage.engine.sensor.SensorManagerTest.setUpBeforeClass(SensorManagerTest.java:98)
    java.lang.ClassFormatError
    at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
    at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:150)
    at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:152)
    at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:139)
    at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:73)
    at mockit.Mockit.setUpMock(Mockit.java:235)
    at com.myapp.MyTest.setUpBeforeClass(MyTest.java:98)

I can't see any pattern as to which tests fail and which don't. I guess this is just a bug in JMockit, but does anybody know a workaround?

I found a very similar question called "Getting ClassFormatError with EMMA?" but the solution doesn't work for me (I'm not using any reentrant=true mock methods). Any other ideas?

Thanks in advance.

A: 

I have been running into the same problem - this seems to have fixed it for me and hopefully will help anybody else as well.

If you're running this through ant, make sure you don't have vars in your javac target's debuglevel argument. The following target will cause the error.

<javac srcdir="${src}" destdir="${bin}" debug="on" debuglevel="lines,source,vars" nowarn="true" />

Change it to:

<javac srcdir="${src}" destdir="${bin}" debug="on" debuglevel="lines,source" nowarn="true">

This is probably a JMockit bug - very subtle and annyoing to find out.

Cem Catikkas
It worked! You're a life-saver! This was something I had given up on as "unfixable" :)
Chris B
A: 

I only saw this question today, but if you can, send me some tests that throw the ClassFormatError when running with EMMA, and I will try to find the bug in JMockit.

By the way, have you tried to use JMockit Coverage? Just add jmockit-coverage.jar to the classpath, and see what you get. Typically, this will produce (without any extra configuration) a nice HTML coverage report in the "coverage-report" dir under the working dir. It can't get any easier than that!

Rogerio
Thanks for the offer, but this problem was on a project at work, and I don't think my company would be pleased with me sending you bits of the source. If I manage to make an example test that exhibits the problem, I'll let you know.I haven't tried JMockit Coverage. I'll check it out! The main reason I use EMMA is that it has good plugins for both Eclipse and Hudson.
Chris B
Hey Rogerio, let me see if I can get you a representative class to repro this.
Cem Catikkas
A: 

My team had the same issue. The concrete case was: using jMockit to mock static methods from a class in a unit-test suite running under TeamCity with EMMA as the coverage tool. The solution was the following:

Add a tear-down method to each test which mocked static methods:

@After
public void tearDown() throws Exception {
  Mockit.tearDownMocks(ClassWithStaticMethods.class);
}
Cd-MaN