使用したJMockitのバージョン:1.21 私はこのようなインターフェースを持っています。 TestInterface:Jmockit:インターフェースフィールドの呼び出し方法を確認してください。
public interface TestInterface {
boolean callMethod();
}
A TestClassを、フィールドがそのインターフェイス 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");
};
}
は、私は偽物interfaceinこのチュートリアルによってメソッドを呼び出すことを検証してみてください。 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が:君たちは気にしない場合は "$ Impl_TestInterfaceはndroid.examples.helloandroid"
を発見した名前 ではありませんクラス、あなたが私に教えてください可能性がありこれを解決する方法どうもありがとう。
私はあなたの「サンプルコード」(* *あなたの[MCVE](http://stackoverflow.com/help/mcve)をコンパイルし、実行していることを確認してください!)でのエラーを修正すると、すべてが正常に働いて、このエラーは表示されませんでした。あなたはあなたの問題を詳述しなければならないでしょう。 – dcsohl
あなたの確認のために@dcsohlに感謝します。現在、私はバージョン1.21で実装しています、それはそのエラーをスローします。 バージョン1.21だけでなく、バージョン1.29でも例外がスローされます。 :D エラーを再現するためにどのバージョンを使用しましたか? –
Androidスタジオでテストします:D –