运行 使用 Mockito 进行黄瓜测试

Run Cucumber test with Mockito

我正在尝试 运行 使用 Mockito 的 JUnit Cucumber 测试。这是我 运行 关注的问题。在我的 Cucumber Runner class 中,我有

@RunWith(Cucumber.class)

在我的常规 JUnit 测试中,我有

@RunWith(Mockito.class)

鉴于我一次只能有一个@RunWith,我如何将Mockito与Cucumber一起使用?

是的,您可以同时使用 Cucumber 和 Mockito。

您不能同时使用两个 JUnit 运行程序。但是,如果您将 Mockito 添加为项目的依赖项并像这样创建模拟:List mockedList = mock(List.class); 那么您应该能够组合这些工具。

可在 http://mockito.org/

获取更多信息

您可以 运行 Mockito 使用 JUnit 规则而不是使用 @RunWith,这样您就可以将 @RunWith 与另一个 JUnit 运行ner 一起使用。

//@RunWith(MockitoJUnitRunner.class)
@RunWith(AnotherRunner.class)
public class TestSomething {
    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    MyMock myMock;
    ...
}