Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Cucumber test with Mockito

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?

like image 612
Steve Avatar asked Jun 23 '16 20:06

Steve


People also ask

Can you use Mockito 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.

Can I use JUnit 5 with Cucumber?

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.


2 Answers

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/

like image 104
Thomas Sundberg Avatar answered Nov 07 '22 01:11

Thomas Sundberg


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;
    ...
}
like image 35
Kin Cheung Avatar answered Nov 07 '22 00:11

Kin Cheung