【发布时间】:2023-04-04 03:21:01
【问题描述】:
使用的 JMockit 版本:1.21 我有这样的界面。 测试接口:
public interface TestInterface {
boolean callMethod();
}
TestClass 有字段是该接口的一个实例 测试类:
public class TestClass {
private final TestInterface inner = new TestInterface() {
@Override
public boolean callMethod() {
subMethod();
return false;
}
};
public void subMethod() { System.out.println("Sub method");
};
}
我尝试在本教程中通过伪造接口来验证调用方法。 http://jmockit.org/tutorial/Faking.html#interfacesd
测试方法。
public class TestInterfaceTest {
TestClass sut;
@Before
public void setUp() {
sut = Deencapsulation.newInstance(TestClass.class);
}
@Test
public void mockAllClassesImplementingAnInterface() {
TestInterface testInterface = new MockUp<TestInterface>() {
@Mock
public boolean callMethod(Invocation inv) {
inv.proceed(); // throw exception here -> Will my expected method be called here?
return true;
}
}.getMockInstance();
Deencapsulation.setField(sut, "INTER", testInterface);
new NonStrictExpectations() {
{
Deencapsulation.invoke(sut, "subMethod");
}
};
Boolean result = Deencapsulation.invoke(Deencapsulation.getField(sut, "INTER"), "callMethod");
assertTrue(result);
new Verifications() {
{
Deencapsulation.invoke(sut, "subMethod"); times = 1;
}
};
}
}
java.lang.IllegalArgumentException:没有名称的类 找到“android.examples.helloandroid.$Impl_TestInterface”
如果你们不介意,请告诉我如何解决这个问题。非常感谢。
【问题讨论】:
-
一旦我修复了您的“示例代码”中的错误(请确保您的 MCVE 编译并运行!),一切正常,我没有看到这个错误.您必须详细说明您的问题。
-
感谢@dcsohl 的确认。当前,我在 1.21 版上实现,它会抛出该错误。不仅是 1.21 版,而且 1.29 版也会引发该异常。 :D 你用什么版本来重现我的错误?
-
我在 Android Studio 上测试:D
标签: java unit-testing junit jmockit