Using RestTemplate in Spring Boot Spock specification with customized Jackson -


i have spring boot app that's using customized jackson objectmapper, , use same customized mapper during testing. used spring boot's method of automatically registering module beans, this:

@configuration public class jacksonconfig {     @bean     public module jodamoneyjackson() {         simplemodule jodamoneymodule = new simplemodule();          jodamoneymodule.adddeserializer(money.class, new jsondeserializer<money>() {             @override             public money deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception {                 return money.parse(p.gettext());             }         });          jodamoneymodule.addserializer(money.class, new tostringserializer());          return jodamoneymodule;     }      @bean public module googleguavajackson() {         return new guavamodule();     } } 

in spock tests resttemplate use "built-in" jackson object mapper automatically registers custom modules. in actual app seems that's used automatically, not in test classes. how can make work tests?

here's test spec looks like:

@webintegrationtest @contextconfiguration(loader = springapplicationcontextloader, classes = myapplication) class sometestspec extends specification {      def resttemplate = new resttemplate()              def "test controller"() {         given:         money expectedtotal = money.of(currencyunit.usd, 100.00)         string request = this.class.classloader.getresource("testinput.json").text          when:         def response = resttemplate.postforobject("http://localhost:8080/test", request, someclass)          then:         response.total == expectedtotal     } } 

i 400 bad request response this, , when debugging can see it's because internally serialization/deserialization isn't using custom module.

as workaround have instantiated own object mapper , registering modules manually, calling mapper.readvalue() string response resttemplate, feels there should better way. input appreciated.

using spock's spring support, provided spock-spring, can have objectmapper injected test specification:

@webintegrationtest @contextconfiguration(loader = springapplicationcontextloader, classes = myapplication) class sometestspec extends specification {      @autowired     objectmapper objectmapper  } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -