jeudi 24 septembre 2015

Spring integration test with mockmvc throws org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

I have a weird problem with a spring integrationtest. I have a working controller and application that runs on tomcat. However in my integration test I get the following stacktrace:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:651)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:610)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:173)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:442)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:430)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:156)
at com.test......
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

My test looks like this:

@Category(DbIntegrationTest.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={RootConfig.class})
@WebAppConfiguration
@ActiveProfiles({"dev", "fk_mock"})
public class WhenUserSubmitsNew  {

@Autowired private WebApplicationContext context;
protected MockMvc mvc;

@Before
public void setup() {
    buildMockMVC();
    createBasicUserSetup();
}

public void buildMockMVC() {

    mvc = MockMvcBuilders
            .webAppContextSetup(context)
            //.apply(springSecurity())
            .build();
}

public void createBasicUserSetup() {
    ...
}


@Test
public void shouldCreate() throws Exception {
    String date1 = "2015-05-05";
    String date2 = "2015-07-30";

    String json = "{\"ids\": [5, 6]}";

    String url = "/submitNew";
    mvc.perform(post(url)
            .with(user(user.getUserName()))
            .sessionAttr(AbstractController.USER_SESSION_ATTR, user)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content(json))
        .andExpect(status().isCreated());
}
} 

And my controller method:

    @RequestMapping(value="/submitNew", method=RequestMethod.POST)
public @ResponseBody Long create(HttpSession session, @RequestBody CustomForm form) {
    ...
}

my gradle.build

apply plugin: 'war'
apply plugin: 'eclipse-wtp'
group = '...'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
   springVersion = '4.2.0.RELEASE'
   springSecurityVersion = '4.0.2.RELEASE'
   springWSVersion = '2.2.2.RELEASE'
   aspectjVersion = '1.6.9'
   slf4jVersion = '1.7.5'
   tilesVersion = '3.0.5'
   jsonPathVersion = '0.8.1'
   servletApiVersion = '3.0.1'
   jacksonDatabindVersion = '2.6.1'
   h2Version = '1.3.156'
   postgresqlVersion = '9.3-1100-jdbc4'
   hibEntitymanagerVersion = '4.3.8.Final'
   //hibValidatorVersion = '4.1.0.Final'
   javaxInjectVersion = '1'
   dbcpVersion = '1.4'
   hamcrestVersion = '1.3'
   junitVersion = '4.11'
   mockitoCoreVersion = '1.9.5'
   jstlVersion = '1.2'
   slf4jAPI     = "org.slf4j:slf4j-api:${slf4jVersion}"
   slf4jJCL     = "org.slf4j:jcl-over-slf4j:${slf4jVersion}"
   slf4jLog4J   = "org.slf4j:slf4j-log4j12:${slf4jVersion}"
   log4JVersion = '1.2.16'
   log4J        = "log4j:log4j:${log4JVersion}"
}
repositories {
   mavenCentral()
}
dependencies {
    providedCompile "javax.servlet:javax.servlet-api:${servletApiVersion}"  
    //finns i båda build
     compile ("org.springframework:spring-webmvc:${springVersion}") {
        exclude group: 'commons-logging', module: 'commons-logging'
   }
   compile "org.springframework:spring-webmvc:${springVersion}"
   compile "org.springframework:spring-orm:${springVersion}"
   compile "org.springframework.security:spring-security-web:${springSecurityVersion}"
   compile "org.springframework.security:spring-security-config:${springSecurityVersion}"
   compile "org.springframework.ws:spring-ws-core:${springWSVersion}"
   compile "org.springframework.security:spring-security-crypto:${springSecurityVersion}"
//   compile 'org.springframework.data:spring-data-jpa:1.9.0.RELEASE'
   compile "com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}"
   //compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.1"
   //compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.1"
   compile "org.apache.tiles:tiles-extras:${tilesVersion}"
   compile "org.apache.tiles:tiles-jsp:${tilesVersion}"
   compile "com.h2database:h2:${h2Version}"
   compile "org.postgresql:postgresql:${postgresqlVersion}"
   compile "org.hibernate:hibernate-entitymanager:${hibEntitymanagerVersion}"
//   compile "org.hibernate:hibernate-validator:${hibValidatorVersion}"
   compile "javax.inject:javax.inject:${javaxInjectVersion}"
   compile "commons-dbcp:commons-dbcp:${dbcpVersion}"
   testCompile "org.hamcrest:hamcrest-all:${hamcrestVersion}"
   testCompile "junit:junit:${junitVersion}"
   testCompile "org.springframework:spring-test:${springVersion}"
   testCompile "org.mockito:mockito-core:${mockitoCoreVersion}"
   testCompile "com.jayway.jsonpath:json-path:${jsonPathVersion}"
   testCompile "com.jayway.jsonpath:json-path-assert:${jsonPathVersion}"
   testCompile "org.springframework.security:spring-security-test:${springSecurityVersion}"
   runtime "javax.servlet:jstl:${jstlVersion}"
      // Logging:
   compile(slf4jAPI)
   runtime(
       slf4jJCL, slf4jLog4J
   )
   runtime(log4J) {
        exclude group: 'javax.mail',   module: 'mail'
        exclude group: 'javax.jms',    module: 'jms'
        exclude group: 'com.sun.jdmk', module: 'jmxtools'
        exclude group: 'com.sun.jmx',  module: 'jmxri'
   }
}

To make things more interesting I also have a unit test for the controller that works which uses standalone setup:

mockMvc = standaloneSetup(controller)
            .setMessageConverters(new MappingJackson2HttpMessageConverter())
            .build();

If anyone has any suggestions on how to move forward it would be extremely appreciated!!

Thanks Karl

Aucun commentaire:

Enregistrer un commentaire