我有以下控制器定义:
public ResponseEntity switch(@PathVariable UUID userId, OAuth2Authentication authentication) { .. }
authentication
对象自动注入,一切都很好。但是一旦我想测试是使用spring的mvc测试框架,我得到:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: argument type mismatch
Method [public org.springframework.http.ResponseEntity com.oauth.controller.SwitchController.switch(java.util.UUID,org.springframework.security.oauth2.provider.OAuth2Authentication)]
Resolved arguments:
[0] [type=java.util.UUID] [value=73e76822-a9d7-4ead-b876-7a4e6fced77f]
[1] [type=org.springframework.security.authentication.TestingAuthenticationToken] [value=org.springframework.security.authentication.TestingAuthenticationToken@a421525e: Principal: CLIENT; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_USER]
并且测试是:
mvc.perform(post("/switch/" + USER.getUuid())
.principal(new TestingAuthenticationToken(clientId, secret, authorities))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
我该如何解决?