Spring 使用sping';mvc测试框架中的默认bean
我有以下控制器定义:Spring 使用sping';mvc测试框架中的默认bean,spring,spring-mvc,spring-mvc-test,Spring,Spring Mvc,Spring Mvc Test,我有以下控制器定义: public ResponseEntity switch(@PathVariable UUID userId, OAuth2Authentication authentication) { .. } 身份验证自动注入对象,一切正常。但一旦我想用spring的mvc测试框架进行测试,我会得到: org.springframework.web.util.NestedServletException: Request processing failed; nested exce
public ResponseEntity switch(@PathVariable UUID userId, OAuth2Authentication 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());
我该如何解决这个问题?典型的错误是不编程到接口,而是编程到具体的类。。。不要使用
OAuth2Authentication
,因为这将使控制器只能在OAuth2环境中运行。相反,请使用这些类的公共接口身份验证。不是编程到接口而是具体类的典型错误。。。不要使用OAuth2Authentication
,因为这将使控制器只能在OAuth2环境中运行。而是使用这些类的公共接口Authentication
。