Mockito验证(...)失败 - “实际上,这个模拟没有互动。”在多个测试中顺序运行

时间:2016-03-05 16:13:59

标签: java mockito testng

我有一个Wrapper类导致而不是equals方法,在包装对象上调用equalsWithoutId方法。在这里实施:

import org.apache.commons.lang3.Validate;

public class IdAgnosticWrapper {

    private final IdAgnostic wrapped;

    public IdAgnosticWrapper(IdAgnostic wrapped) {
        Validate.notNull(wrapped, "wrapped must not be null");
        this.wrapped = wrapped;
    }

    @Override
    public int hashCode() {
        return wrapped.hashCodeWithoutId();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof IdAgnosticWrapper)) {
            return false;
        }
        return wrapped.equalsWithoutId(((IdAgnosticWrapper) obj).getWrapped());
    }

    public IdAgnostic getWrapped() {
        return wrapped;
    }
}

IdAgnostic是一个简单的界面,可确保存在所需的方法

public interface IdAgnostic {
    int hashCodeWithoutId();
    boolean equalsWithoutId(Object o);
}

然后我有一些应该测试的单元测试,如果equals()委托给包装的#equalsWithoutId()方法,hashCode()委托给包装的#hashCodeWithoutId。

现在我尝试通过这些测试来测试它

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.verify;

public class IdAgnosticWrapperTest {

    @Mock
    private IdAgnostic wrappedMock;

    @InjectMocks
    private IdAgnosticWrapper tested;


    @BeforeMethod
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testEquals_EqualsWithoutIdIsCalledOnWrapped() throws Exception {
        tested.equals(tested);
        verify(wrappedMock).equalsWithoutId(tested.getWrapped());
    }

    @Test
    public void testHashCode_HashCodeWithoutIdIsCalledOnWrapped() throws Exception {
        tested.hashCode();
        verify(wrappedMock).hashCodeWithoutId(); //line 34
    }
}

正如你所看到的,我只是创建包装模拟和测试,无论equals和hashcode是否委托了这些功能。

如果我单独运行测试,一切正常,但如果我按顺序运行两个测试,第二个测试将失败并显示此消息

Wanted but not invoked:
wrappedMock.hashCodeWithoutId();
-> at com.my.app.utils.IdAgnosticWrapperTest.testHashCode_HashCodeWithoutIdIsCalledOnWrapped(IdAgnosticWrapperTest.java:34)
Actually, there were zero interactions with this mock.

为什么会这样?

1 个答案:

答案 0 :(得分:1)

实际上,这里发生的是@InjectMocks无法正确注入构造函数参数wrapped。根据{{​​3}},这是当前的行为。因此,除非您想使用setter注入,否则您需要删除@InjectMocks注释。请参阅修订后的代码:

public class IdAgnosticWrapperTest {

  @Mock
  private IdAgnostic wrappedMock;

  private IdAgnosticWrapper tested;

  @BeforeMethod
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);

    this.tested = new IdAgnosticWrapper(this.wrappedMock);
  }

  @Test
  public void testEquals_EqualsWithoutIdIsCalledOnWrapped()
      throws Exception {
    tested.equals(tested);
    verify(wrappedMock).equalsWithoutId(tested.getWrapped());
  }

  @Test
  public void testHashCode_HashCodeWithoutIdIsCalledOnWrapped()
      throws Exception {
    tested.hashCode();
    verify(wrappedMock).hashCodeWithoutId(); //line 34
  }
}

当我运行上面的代码时,两个测试按顺序一起运行并传递。