使用Guice,如何将单元测试中的模拟对象注入到要测试的类中

Mar*_*iam 1 java dependency-injection guice mockito

考虑以下代码:

@Singleton
public class MyServiceImpl {
    public int doSomething() {
        return 5;
    }
}

@ImplementedBy(MyServiceImpl.class)
public interface MyService {
    public int doSomething();
}

public class MyCommand {
    @Inject private MyService service;

    public boolean executeSomething() {
        return service.doSomething() > 0;
    }
}

public class MyCommandTest {
    @InjectMocks MyServiceImpl serviceMock;
    private MyCommand command;

    @Before public void beforeEach() {
        MockitoAnnotations.initMocks(this);
        command = new MyCommand();
        when(serviceMock.doSomething()).thenReturn(-1); // <- Error here
    }

    @Test public void mockInjected() {
        boolean result = command.executeSomething();
        verify(serviceMock).doSomething();
        assertThat(result, equalTo(false));
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的模拟实现对象上添加doSomething()方法时,我的测试失败了。我得到错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles())。thenReturn(articles);

另外,可能会由于以下原因而出现此错误:1.您对以下方法之一进行存根:final / private / equals()/ hashCode()方法。这些方法不能存根/验证。不支持在非公共父类上声明的模拟方法。2.在when()内部,您不会在模拟对象上调用方法,而是在其他某个对象上调用方法。

我是通过Guice进行依赖注入的新手,并且不确定为什么我不能以这种方式模拟实现对象吗?

tkr*_*use 8

不使用CDI进行测试

一个简单的解决方案是将CDI与构造函数注入结合起来,而无需为测试做准备Guice:

public class MyCommand {
    private final MyService service;

    @Inject
    public MyCommand(MyService service) {
        this.service = service;
    }

    public boolean executeSomething() {
        return service.doSomething() > 0;
    }
}

@RunWith(MockitoJUnitRunner.class)
public class MyCommandTest {
    @Mock
    MyServiceImpl serviceMock;
    private MyCommand command;

    @Before public void beforeEach() {
        MockitoAnnotations.initMocks(this);
        when(serviceMock.doSomething()).thenReturn(-1); // <- Error here

        // inject without Guice
        command = new MyCommand(serviceMock);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用Mockito进行CDI测试

否则,如果您不喜欢构造函数注入,则测试代码应如下所示:

@RunWith(MockitoJUnitRunner.class)
public class MyCommandTest {
    @Mock
    MyServiceImpl serviceMock;
    @InjectMocks 
    private MyCommand command;

    @Before 
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
        command = new MyCommand();
        when(serviceMock.doSomething()).thenReturn(-1); // <- Error here
    }
}
Run Code Online (Sandbox Code Playgroud)