Skip to content

Capturing interfaces fails because of parameter order #20

@nsinkov

Description

@nsinkov

I have two interfaces and I am mocking one method on each one. I'm using the @capturing annotation. But simply having the @mocked parameters in a different order causes one of the interfaces to not be mocked.

(The same happens if the @mocked parameters are fields of the test class - the order matters)

The code below has two identical tests. One fails only because the parameters are in a different order. (I believe that both tests should pass)

code:

import mockit.Capturing;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.integration.junit4.JMockit;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JMockit.class)
public class InterfaceMocks {

public static interface I1 {
    Double num();
    String other();
}

public static interface I2 {
    I1 getI1();
    String method();
}

@Test
public void testGood(@Mocked("num") @Capturing final I1 i1Mock, @Mocked("getI1") @Capturing final I2 i2Mock) {
    new NonStrictExpectations() {
        {
            i1Mock.num();
            result = null;

            i2Mock.getI1();
            result = null;
        }
    };

    I2 i2 = new I2() {
        @Override
        public I1 getI1() {
            throw new RuntimeException();
        }

        @Override
        public String method() {
            throw new RuntimeException();
        }
    };

    I1 i1 = i2.getI1();

}

@Test
public void testBad(@Mocked("getI1") @Capturing final I2 i2Mock, @Mocked("num") @Capturing final I1 i1Mock) {
    new NonStrictExpectations() {
        {
            i1Mock.num();
            result = null;

            i2Mock.getI1();
            result = null;
        }
    };

    I2 i2 = new I2() {
        @Override
        public I1 getI1() {
            throw new RuntimeException();
        }

        @Override
        public String method() {
            throw new RuntimeException();
        }
    };

    I1 i1 = i2.getI1();

}

}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions