Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$advice catch-all mock method does not work if it is the only mock method #171

hontvari opened this issue May 4, 2015 · 1 comment


Copy link

@hontvari hontvari commented May 4, 2015

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;

    public final void test() {

        new MockB();

        new B().fn1();


    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> {
        Object $advice(Invocation invocation) {
            System.out.println("Mock advice called, on "
                    + invocation.getInvokedMember().getName());
            mockCalled = true;
            return null;

        // without this it does not work
        void fn2() {
            System.out.println("Mock fn2 called");
            mockCalled = true;


@rliesenfeld rliesenfeld added the bug label May 4, 2015
@rliesenfeld rliesenfeld self-assigned this May 4, 2015
Copy link

@rliesenfeld rliesenfeld commented May 4, 2015

Well spotted! It's a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

No branches or pull requests

2 participants