Spring Check boot 1.4 with security enabled?

I am wondering how do I go through user authentication for my tests? Currently, all tests that I write will fail because endpoints require authorization.

Test code:

@RunWith(SpringRunner.class)
@WebMvcTest(value = PostController.class)
public class PostControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private PostService postService;

    @Test
    public void testHome() throws Exception {
        this.mvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("posts"));
    }


}

One of the solutions found is to disable it by setting secure to false in @WebMvcTest. But that is not what I am trying to do.

Any ideas?

+4
source share

All Articles