The $advice mock method only works if there is at least one another, usual mock method too.
For example the following test does work. It shows that the mock methods are running. The fn2 mock method at the end has no role in the test. However if I comment out this unnecessary fn2 mock method, the test fails. The output on STDOUT will show that the mock method is not active at all, instead of it, the real fn1 method is called.
package x;
import static org.junit.Assert.*;
import mockit.Invocation;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;
public class ATest {
static boolean mockCalled = false;
@Test
public final void test() {
new MockB();
new B().fn1();
assertTrue(mockCalled);
}
static class B {
void fn1() {
System.out.println("Real fn1 called");
}
void fn2() {
System.out.println("Real fn2 called");
}
}
static class MockB extends MockUp<B> {
@Mock
Object $advice(Invocation invocation) {
System.out.println("Mock advice called, on "
+ invocation.getInvokedMember().getName());
mockCalled = true;
return null;
}
// without this it does not work
@Mock
void fn2() {
System.out.println("Mock fn2 called");
mockCalled = true;
}
}
}
The $advice mock method only works if there is at least one another, usual mock method too.
For example the following test does work. It shows that the mock methods are running. The fn2 mock method at the end has no role in the test. However if I comment out this unnecessary fn2 mock method, the test fails. The output on STDOUT will show that the mock method is not active at all, instead of it, the real fn1 method is called.