我最近开始使用JMockit并且在TDD和嘲笑方面非常新。我喜欢使用代码覆盖来确保我已经测试了给定类/方法的所有行。
我在尝试JMockit(Delegate()功能)和代码覆盖时遇到了以下错误。我包括传球和失败的测试。
我不确定我做错了什么?
我想知道是否有其他人遇到此问题,是否有可用的解决方法或修复方法?
我甚至不确定这是否是JMockit问题或EclEmma问题。 感谢。
我的设置:
失败追踪:
java.lang.IllegalArgumentException:找不到兼容的方法: getType(java.lang.Integer)at com.ps.jmockit.samples.DelegateCoverageTest $ Dog.getAnimalType(DelegateCoverageTest.java:99) 在 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) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
代码:(coverageFails()将在与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);
}
}
}
答案 0 :(得分:1)
似乎是一个错误:我在此处看到相关链接:https://github.com/jacoco/eclemma/issues/9和此处:https://github.com/jacoco/jacoco/issues/35