Spring Security @WithMockUser does not work with cucumber testing

I use cucumber tests to test my spring boot application with spring protection enabled. The work works fine, except when I run my test suite with a cucumber, test some tests using spring security, for example.

    @WithMockUser(username = "BROWSER", roles =
   {"BROWSER","ADMIN"})

fail. These tests work if I execute them in seclusion as simple junit tests, but do not run when executed with the steps of a cucumber test. The problem is this: spring security layout validation behavior is not applied when I run the same tests with cucumbers.

My cucumber test test class below

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources", monochrome = true, format =
{"pretty", "html:src/main/resources/static/cucumber"})
public class CucumberTests
{

}

, Maven <reuseForks>false</reuseForks>. maven, .

UPDATE

AbstractIntegrationTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Services.class,loader = SpringApplicationContextLoader.class)
//@IntegrationTest
@WebIntegrationTest(randomPort = true)
public abstract class AbstractIntegrationTest {

, , theses - ,

@When("^I apply a GET on (.*)$")
    @WithMockUser(username = "BROWSER", roles = { "BROWSER", "ADMIN" })
    public void i_search_with_rsql(String query) throws Throwable {
    result = mvc.perform(get(uri, query));
    }

.

+4
1

, Spring . MockMvc Spring :

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;

// ...

@Before
public void setup() {
    mvc = MockMvcBuilders
            .webAppContextSetup(context)
            .apply(springSecurity()) // ADD THIS!
            .build();
}
0

All Articles