How do I unit test spring security @PreAuthorize custom expression

后端 未结 1 1370
情歌与酒
情歌与酒 2021-01-23 09:48
    @PostMapping
        @ResponseStatus(HttpStatus.CREATED)
        @PreAuthorize("@messageSecurityService.isAuthorized(#userAuthentication)")
        public          


        
1条回答
  •  被撕碎了的回忆
    2021-01-23 10:29

    Have you stub the return of the @MockBean e.g

    @MockBean(name = "messageSecurityService")
    public MessageSecurityService messageSecurityService;
    
    @Test
    public void testing(){
     when(messageSecurityService.isAuthorized(anyString())).thenReturn("somethingHere");
     //rest of your assertions
    }
    

    also have you added the following in your test class:

    @BeforeEach
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
    

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