$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

Comments

2 participants
@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;

    @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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld May 4, 2015

Member

Well spotted! It's a bug.

Member

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