Partial mocking of a delegate class #268

Closed
oazmon opened this Issue Mar 15, 2016 · 2 comments

Projects

None yet

2 participants

@oazmon
oazmon commented Mar 15, 2016

Please provide the following information:

  • Version of JMockit that was used: 1,21

  • Description of the problem or enhancement request:
    When attempting to test a delegate class, and an highly simplified case would be:
    public static class MyClass implements Runnable {
    private Runnable r;

    public MyClass(Runnable r) {
        this.r = r;
    }
    
    public Runnable getRun() {
        return r;
    }
    
    public void run() {
        getRun().run();
    }
    

    }

And attempting to perform a partial mock on the class to isolate in the above example the run() method from the getRun() method, and example test would be:

@Test
public void testMe(final @Injectable Runnable mockedRunnable) {
    final MyClass instance = new MyClass(mockedRunnable);
    new Expectations(instance) {
        {
            instance.getRun();
            result = new Runnable() {
                public void run() {
                    System.out.print("Expectation runnable");
                }
            };
        }
    };

    instance.run();
}

One gets: java.lang.IllegalArgumentException: Already mocked: interface java.lang.Runnable
Removing the extends Runnable on the class solves the error, but makes the delegate useless.

Any ideas?

@rliesenfeld rliesenfeld added the bug label Mar 16, 2016
@rliesenfeld rliesenfeld self-assigned this Mar 16, 2016
@rliesenfeld
Member

It's a bug; should be fixed by the time the next release is out.

@oazmon
oazmon commented Mar 18, 2016

Need any help?

@rliesenfeld rliesenfeld added a commit that closed this issue Mar 19, 2016
@rliesenfeld rliesenfeld Fixes bug where the partial mocking of a class implementing a mocked …
…interface would fail with an "Already mocked" exception; closes #268.
fe4efd2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment