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

Closed
hontvari opened this issue May 4, 2015 · 1 comment
Closed
Assignees
Labels

Comments

@hontvari
Copy link

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;

    @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;
        }

    }

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

Well spotted! It's a bug.

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

No branches or pull requests

2 participants