Stack Exchange
Mocking in Cucumber with Spring boot
While running the following cucumber test, I'm not able to mock the designationService. The exception is WebApplicationContext is required at line 
this.mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).apply(springSecurity(springSecurityFilterChain)).build();

. WebApplicationContext  is not getting autowired. The entire code is: 
package steps;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static 
org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;

import javax.servlet.Filter;

import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

import com.exampletest.subpkg.employee.serviceimpls.DesignationServicesImpl;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;



@SpringBootTest
@ContextConfiguration(loader = SpringBootContextLoader.class)
@WebAppConfiguration
    public class DeleteDesignationById {

    String URI;
    HttpHeaders headers = new HttpHeaders();
    //RestTemplate restTemplate = new RestTemplate();
    private ResponseEntity<String> response;
    String token = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJNYW5pIiwic2VjcmV0IjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJleHAiOjE1NzQxOTIxNjYsImlhdCI6MTU3NDE3NDE2Nn0._cwqQ65_N9qL1cV79zaAsiMbR2Y0eMQFG642Evb90gs";



    private DesignationServicesImpl designationService=Mockito.mock(DesignationServicesImpl.class);

    @Autowired
    private WebApplicationContext webAppContext;

    @Autowired
    private Filter springSecurityFilterChain;

    private MockMvc mockMvc;

    @BeforeEach
    public void setUp() {
        this.mock = MockMvcBuilders.webAppContextSetup(webAppContext).apply(springSecurity(springSecurityFilterChain))
                .build();
    }

    @Given("^I set service endpoint for Delete Designation$")
    public void i_set_service_endpoint_for_Delete_Designation() throws Throwable {

        URI = "http://localhost:8080/api/designation/1";

    }

    @When("^header is with Jwt token for Delete Request$")
    public void header_is_with_Jwt_token_for_Delete_Request() throws Throwable {

        headers.add("Authorization", token);

    }

    @When("^I send Delete Request for Designation with its Id$")
    public void i_send_Delete_Request_for_Designation_with_its_Id() throws Throwable {

//      when(designationService.deleteDesignationById(Mockito.anyLong())).thenReturn("deleted");

//      HttpEntity<String> entity = new HttpEntity<>(null,headers);
//        response=restTemplate.exchange(URI, HttpMethod.DELETE, entity, String.class);
//        assertThat(response).isEqualTo("delete");

        String uri = "/api/designation/1";

        when(
                designationService
                .deleteDesignationById(Mockito.anyLong()))
        .thenReturn("deleted");
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).apply(springSecurity(springSecurityFilterChain))
                .build();
         mockMvc.perform(
                MockMvcRequestBuilders
                .delete(uri)
                .header("Authorization", token))
                .andReturn();
        //int status = getResponse().getStatus();
        //assertEquals(200, status);

    }

    @Then("^Valid http response of (\\d+) recieved$")
    public void valid_http_response_of_recieved(int arg1) throws Throwable {
        HttpStatus statusCode = response.getStatusCode();
        assertEquals(statusCode.value(), arg1);
    }

}

The Exception trace is as follows:
java.lang.IllegalArgumentException: WebApplicationContext is required
at org.springframework.util.Assert.notNull(Assert.java:198)
at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.<init>(DefaultMockMvcBuilder.java:52)
at org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup(MockMvcBuilders.java:51)
at steps.DeleteDesignationById.i_send_Delete_Request_for_Designation_with_its_Id(DeleteDesignationById.java:91)
at ✽.When I send Delete Request for Designation with its Id(designation.feature:22)


BeforeEach is a JUnit 5 annotation. Not a Cucumber annotation.

I changed that "BeforeEach"  to "Before", but still the error is same.
