I recently started using JMockit and am very new at TDD and mocking. I like to use code coverage to make sure that I have tested all the lines for a given a class/method.
I came across the following error when trying JMockit (Delegate() functionality) and code-coverage. I am including both a passing and failing test.
I am not sure if I am doing something wrong?
I would like to know if anyone else has encountered this issue and if there are any workaround or fixes available?
I am not even sure if this is a JMockit issue or EclEmma issue. Thanks.
My setup:
Failure Trace:
java.lang.IllegalArgumentException: No compatible method found: getType(java.lang.Integer) at com.ps.jmockit.samples.DelegateCoverageTest$Dog.getAnimalType(DelegateCoverageTest.java:99) at com.ps.jmockit.samples.DelegateCoverageTest.coverageFails(DelegateCoverageTest.java:71) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:601) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:601) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Code: (coverageFails() will show the error above when used with EclEmma)
package com.ps.jmockit.samples;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import mockit.Delegate;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Test;
/**
* As of 7/25/2013, if I try to run the EclEmma coverage tool, it fails for {@link #coverageFails()}
*/
public class DelegateCoverageTest
{
@Mocked
Animal fakeAnimal;
@Test
public void coverageWorks()
{
final String fake = "Fake";
new Expectations()
{
{
DelegateCoverageTest.this.fakeAnimal.getType(1);
result = fake;
}
};
//Arrange
final Dog dog = new Dog();
// Act
final String animalType = dog.getAnimalType(this.fakeAnimal);
// Assert
assertThat(animalType, is(fake));
}
@Test
public void coverageFails()
{
final String one = "One";
final String two = "Two";
new Expectations()
{
{
DelegateCoverageTest.this.fakeAnimal.getType(anyInt);
result = new Delegate()
{
@SuppressWarnings("unused")
String aDelegateMethod( final int input )
{
return input == 1
? one
: two;
}
};
}
};
//Arrange
final Dog dog = new Dog();
// Act
final String animalType = dog.getAnimalType(this.fakeAnimal);
// Assert
assertThat(animalType, is(one));
}
//----------------- SUPPORTING CLASSES -----------------//
interface Animal
{
String getType(int input);
}
class Mammal implements Animal
{
@Override
public String getType(final int input)
{
return "Mammal";
}
}
class Dog
{
public String getAnimalType(final Animal animal)
{
return animal.getType(1);
}
}
}
Seems to be a bug : I see relevant links here: https://github.com/jacoco/eclemma/issues/9 and here: https://github.com/jacoco/jacoco/issues/35
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With