Unit test Springboot MockMvc returns 403 Forbidden

后端 未结 2 1533
余生分开走
余生分开走 2021-01-12 15:02

I wrote one unit test that tests UsersController. UsersControllerTest.findUser is working fine, but UsersControllerTest.insertGetModifyDelete it doesn\'t.

In the log

相关标签:
2条回答
  • 2021-01-12 15:30

    You can try to debug this program.i think probleam is happend in "mockMvc" object is not autowired.mockMvc object should load from WebApplicationContext in before program run.

    @Autowired
    private WebApplicationContext webApplicationContext
    
    @Before()
    public void setup()
    {
        //Init MockMvc Object and build
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    
    0 讨论(0)
  • 2021-01-12 15:44

    I got around it with @AutoConfigureMockMvc(addFilters = false). Please make sure that this won't compromise other functionality on your HTTP stack.

    0 讨论(0)
提交回复
热议问题