Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用sping';mvc测试框架中的默认bean_Spring_Spring Mvc_Spring Mvc Test - Fatal编程技术网

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