我有一个MockMVC测试,我正在尝试像这样利用控制器方法匹配器:
LocalBroadcastManager
这应该允许我验证测试调用ManagerController的@Test
public void environmentTopology() throws Exception {
mockMvc.perform(get("/topology/{environmentId}", "envB"))
.andExpect(handler().methodCall(on(ManagerController.class)
.environmentTopology("envB", true)));
}
方法。但是我得到以下异常:
environentTopology
答案 0 :(得分:11)
解决方案的提示是https://github.com/spring-projects/spring-hateoas/issues/155#issuecomment-36487869
方法到链接功能的工作方式是为方法的返回类型创建代理,以便能够检查先前的调用。由于String是最终类,因此无法按定义代理。
检查方法签名:
java.lang.IllegalArgumentException: Cannot subclass final class java.lang.String
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:565)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.initProxy(MvcUriComponentsBuilder.java:611)
at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.access$000(MvcUriComponentsBuilder.java:89)
at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept(MvcUriComponentsBuilder.java:725)
由于它返回一个String,你需要将它包装在一个可代理的类中,如@RequestMapping(value = "/topology/{environmentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String environmentTopology(@PathVariable("environmentId") String environmentId, ...
ResponseEntity
@RequestMapping(value = "/topology/{environmentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<String> environmentTopology(@PathVariable("environmentId") String environmentId,
包含您的ResponseEntity
响应,允许测试机制正常工作。