1

I have the following controller definition:

public ResponseEntity switch(@PathVariable UUID userId, OAuth2Authentication authentication) { .. }

authentication object injected automatically, everything is fine. But once I want to test is with spring's mvc test framework I get:

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]

and test is:

mvc.perform(post("/switch/" + USER.getUuid())
     .principal(new TestingAuthenticationToken(clientId, secret, authorities))
     .andExpect(status().isOk())
     .andDo(MockMvcResultHandlers.print());

How can I solve it?

nKognito
  • 6,297
  • 17
  • 77
  • 138
  • classic mistake of not programing to interfaces but concrete classes... Don't use `OAuth2Authentication` as that will tie your controller to only function in an OAuth2 environment. Instead use `Authentication` the common interface for those classes. – M. Deinum Jun 21 '16 at 10:53

0 Answers0