Search code examples
javaspringmockitocucumbercucumber-java

@MockBean injected correctly but with null value on test class


I am using tests with Cucumber and Spring Boot

@CucumberContextConfiguration
@ActiveProfiles(profiles = { "cucumber" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ExtendWith(SpringExtension.class)
public class FooTest {

    @Autowired
    BatchService batchService;

    @MockBean
    S3ClientService s3ClientService;

    @MockBean
    HttpClientService httpClientService;

    @SpyBean
    UndueService undueService;


    @Given("^foo cucumber test$")
    public void foo_cucumber_test() {
        System.out.println("Foo Test");
    }
}

When I run/debug my test with a break point on @Given method

enter image description here

I got this weird behavior, the @Mockbean/@SpyBean are correctly injected but in the test class its values are null !! and I can't run Mockito functions verify or when

But when I run a test without cucumber

@Test
void fooTest() {
    System.out.println("Foo Test");
}

It works fine !! the values are not null


Solution

  • So, mock beans are created but not injected to the test suite. I guess you should put both @Autowired and @MockBean annotations.

    Here is a similar question: Spring @MockBean not injected with Cucumber