5
votes

I'm trying to run a JUnit Cucumber test that uses Mockito. Here's the issue I'm running into. In my Cucumber Runner class, I have

@RunWith(Cucumber.class)

And in my regular JUnit tests I have

@RunWith(Mockito.class)

Given that I can only have one @RunWith at a time, how can I use Mockito in conjunction with Cucumber?

2

2 Answers

7
votes

Yes, you can use Cucumber and Mockito at the same time.

You can't use two JUnit runners at the same time. But if you add Mockito as a dependency to your project and create your mocks like this: List mockedList = mock(List.class); then you should be able to combine the tools.

More information is availabe at http://mockito.org/

6
votes

You can run Mockito using JUnit rule instead of using @RunWith so that you can use @RunWith with another JUnit runner.

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

    @Mock
    MyMock myMock;
    ...
}