Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Cannot subclass final class java.lang.String' when using Spring Boot 1.4 MockMVC controller method matching

I have a MockMVC test and I'm trying to leverage the controller method matcher like so:

@Test
public void environmentTopology() throws Exception {
    mockMvc.perform(get("/topology/{environmentId}", "envB"))
            .andExpect(handler().methodCall(on(ManagerController.class)
                 .environmentTopology("envB", true)));
}

This should allow me to verify that the ManagerController's environentTopology method is called by the test. However instead I get the following exception:

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)
like image 516
checketts Avatar asked Aug 03 '16 19:08

checketts


1 Answers

The hint to a solution is here https://github.com/spring-projects/spring-hateoas/issues/155#issuecomment-36487869

The way the method-to-link functionality works is by creating a proxy for the return type of the method to be able to inspect the previous invocation. As String is a final class it cannot be proxied by definition.

Checked the method signature:

@RequestMapping(value = "/topology/{environmentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String environmentTopology(@PathVariable("environmentId") String environmentId, ...

Since it returns a String, you need to wrap it in a class that is proxy-able, like ResponseEntity

@RequestMapping(value = "/topology/{environmentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<String> environmentTopology(@PathVariable("environmentId") String environmentId,

The ResponseEntity wraps your String response allowing the test mechanism to work.

like image 75
checketts Avatar answered Nov 05 '22 21:11

checketts