私は通常、Expectations
を使用して、異なる戻り値で値をモックすることを理解しています。たとえば:結果ブロックのExpectationsブロックは、検証ブロックと同じものですか?
new Expectations() {{
bar.getGreeting();
result = "Hello, world!";
times = 2;
}};
私は気づいたが、result
はオプションです。この時点で、このブロックはメソッドが2回呼び出されたことを確認し、そうでない場合はMissingInvocation
エラーをスローします。例えばので:
@Test
public void testRunFoo(@Mocked final Bar bar) {
Foo foo = new Foo(bar);
new Expectations() {{
bar.runBar();
times = 2;
}};
foo.runFooWithBarTwice(); //Successful
//foo.runFooWithoutBar(); //Will throw a MissingInvocationException
}
私はこのコードではなくVerifications
を使用するのと同じもののように見えることに気づいた:
@Test
public void testRunFoo(@Mocked final Bar bar) {
Foo foo = new Foo(bar);
foo.runFooWithBarTwice(); //Successful
//foo.runFooWithoutBar(); //Will throw a MissingInvocationException
new Verifications() {{
bar.runBar();
times = 2;
}};
}
はVerifications
ブロックと同じものでは結果なしExpectations
ブロックですか?あなたの好みに合わせてどちらかを使用できますか?それとも私が紛失している2つの微妙な違いがありますか?