I understand that in general, Expectations
are used to mock values with differnt return values. For instance:
new Expectations() {{
bar.getGreeting();
result = "Hello, world!";
times = 2;
}};
I have noticed that result
is optional though. At that point, this block just confirms that the method was called twice and throws a MissingInvocation
error if it is not. So for instance:
@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
}
I have noticed that this code appears to be the same thing as using Verifications
instead:
@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;
}};
}
Is an Expectations
block without a result the same thing as a Verifications
block? Can you use either according to your personal preference? Or is there some subtle difference between the two that I'm missing?