Spring @MockBean not injected with Cucumber
<p>I'm implementing a <code>SchedulerService</code> that uses a <code>AgentRestClient</code> bean to get some data from an external system. It looks something like this:</p>

<pre><code>@Service
public class SchedulerService {

  @Inject
  private AgentRestClient agentRestClient;

  public String updateStatus(String uuid) {
    String status = agentRestClient.get(uuid);
    ...
  }
  ...
}
</code></pre>

<p>To test this service, I'm using Cucumber while at the same time I'm trying to mock the behavior of <code>AgentRestClient</code> using Spring Boot's <code>@MockBean</code> annotation, as follows:</p>

<pre><code>import cucumber.api.CucumberOptions;
import cucumber.api.java.Before;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CentralApp.class)
@CucumberOptions(glue = {"com.company.project.cucumber.stepdefs", "cucumber.api.spring"})
public class RefreshActiveJobsStepDefs {

  @MockBean
  private AgentRestClient agentRestClient;

  @Inject
  private SchedulerService schedulerService;

  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    given(agentRestClient.get(anyString())).willReturn("FINISHED");//agentRestClient is always null here
  }

  //Skipping the actual Given-When-Then Cucumber steps...
}
</code></pre>

<p>When I try to run any Cucumber scenario, the <code>agentRestClient</code> is never mocked/injected. The <code>setUp()</code> method fails with a NPE:</p>

<pre><code>java.lang.NullPointerException
  at com.company.project.cucumber.stepdefs.scheduler.RefreshActiveJobsStepDefs.setUp(RefreshActiveJobsStepDefs.java:38)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at cucumber.runtime.Utils$1.call(Utils.java:37)
  at cucumber.runtime.Timeout.timeout(Timeout.java:13)
  at cucumber.runtime.Utils.invoke(Utils.java:31)
  at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
  at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:223)
  at cucumber.runtime.Runtime.runHooks(Runtime.java:211)
  at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:201)
  at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
  at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
  at cucumber.runtime.Runtime.run(Runtime.java:121)
  at cucumber.api.cli.Main.run(Main.java:36)
  at cucumber.api.cli.Main.main(Main.java:18)
</code></pre>

<p>To get to this point I followed the following 2 resources, but still no luck getting it working:</p>

<ul>
<li><a href="https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4" rel="noreferrer">https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4</a></li>
<li><a href="http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html" rel="noreferrer">http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html</a></li>
</ul>

<p>The culprit seems to be Cucumber integration into Spring, because when I try the same approach with a plain JUnit <code>@Test</code> method, mocking works as expected.</p>

<p>So could you please tell me what Cucumber or Spring configuration have I missed or misunderstood?</p>

<p>Thanks,
Bogdan</p>