5

There are ways online to integrate Cucumber with Spring Boot. But I cannot find how to do so with Mockito also. If I use the Cucumber runner and annotate the steps file with ContextConfiguration and SpringBootTest, the container injects the Autowired dependencies and its all fine. The problem is that dependencies annotated with Mock, MockBean and InjectMocks dont work. Anyone knows why it doesnt work and how to make it work?

EDIT: It is possible to instantiate the bean with mock(Bean.class), instead of using the Mock annotation. But what about features like MockBean and InjectMocks?

Runner

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:build/cucumber_report/"},
                 features = "classpath:cucumber/",
                 glue = {"com.whatever"},
                 monochrome = true,
                 dryRun = false)
public class CucumberTest {

}

Steps

@ContextConfiguration
@SpringBootTest
public class CucumberSteps
{

    @Autowired
    private Bean bean;

    @InjectMocks //doesnt work
    private AnotherBean anotherBean;

    @MockBean //doesnt work with @Mock also
    MockedBean mockedBean;


    @Given("^Statement$")
    public void statement() throws Throwable {
        MockitoAnnotations.initMocks(this); //doesnt work with or without this line
        Mockito.when(mockedBean.findByField("value"))
               .thenReturn(Arrays.asList());
    }

    //Given-When-Then   
}
Roger Gouveia
  • 431
  • 5
  • 7
  • https://stackoverflow.com/questions/38001759/run-cucumber-test-with-mockito – Dirk Deyne Jun 18 '18 at 18:45
  • Yes @DirkDeyne , it is possible to manually do a "mock(Bean.class)" instead of the Mock annotation. But how can I use features like MockBean and InjectMocks? – Roger Gouveia Jun 18 '18 at 19:52

1 Answers1

1

Runner :

@CucumberOptions(plugin = {"pretty"}, 
                 glue = {"com.cucumber.test"},
                 features = "x/y/resources")
public class CucumberTest { 

}

Here we will create a class with @SpringBootTest, @RunWith(SpringRunner.class) to start load beans into spring context. Now will mock the spring beans whatever we want to mack at here

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringTest {

@MockBean    
private Mockedbean mockedbean;
}

Now, we need to extend the SpringBootTest annotated testclass to CucumberSteps class, then autowire the mocked bean at here, will get the instance of mocked bean(Mockedbean). We can do autowire and get instance of other spring boot beans also(TestBean)

public class CucumberSteps extends SpringTest {

@Autowired
private Mockedbean mockedbean;

@Autowired
private TestBean testBean;

}

  • 1
    Hi, I've got the same problem but this doesn't work for me. I'm using multiple steps definitions since cucumber now only allows one application context configuration, I'm only able to extend or mark a class with SpringBoot test to build the application context. So in my case Mockito only works in the class that extends or has the SpringBootTest annotation, when the other step is executed I've got is not a mock exception when I try to do a verify for instance. – Antonio682 Jan 16 '19 at 11:49
  • 1
    It worked for me perfectly, Thanks! PS: with feature = "x/y/resources" (and not 'java') and my base application package on glue.. (not a generic com.cucumber.test). this Autowired CocuberFeatureTest and @MockBean declared on parent did the trick – wdavilaneto Oct 14 '19 at 16:28