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?
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.
To use JUnit to execute cucumber scenarios add the cucumber-junit dependency to your pom. Note that cucumber-junit is based on JUnit 4. If you're using JUnit 5, use the cucumber-junit-platform-engine. Or include junit-vintage-engine dependency, as well.
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/
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;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With