Reputation: 12039
I have an application that queries EC2 Instance
data using Amazon's AWS SDK for Java. The Instance is serialized to a JSON String using the Jackson
wrapper class included in the AWS SDK. Later, I'm able to use the Jackson.fromJsonString(String, Class)
method to deserialize the JSON String back into an EC2 Instance
object.
This all works great in my application code. However, it fails every single time when run from a JUnit test within Eclipse. I'm using the exact same data, and the exact same deserialization code. When I run it from a JUnit test, though, I get the following Exception:
com.amazonaws.SdkClientException: Unable to parse Json String.
at com.amazonaws.util.json.Jackson.fromJsonString(Jackson.java:66)
at com.myapp.filters.test.AbstractResourceFilterCriteriaTest.testMeetsCriteria_Fail(AbstractResourceFilterCriteriaTest.java:166)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "instanceType": com.amazonaws.services.ec2.model.Instance#setInstanceType(1 params) vs com.amazonaws.services.ec2.model.Instance#setInstanceType(1 params)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:461)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3838)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3732)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
at com.amazonaws.util.json.Jackson.fromJsonString(Jackson.java:64)
... 26 more
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "instanceType": com.amazonaws.services.ec2.model.Instance#setInstanceType(1 params) vs com.amazonaws.services.ec2.model.Instance#setInstanceType(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:300)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:619)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:515)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:256)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:169)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:403)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
... 33 more
Any idea what on earth could be going wrong here? Why is it that it works just fine in the main application code, but fails when run as a JUnit test? Since I don't own the Instance
object (it's part of Amazon's SDK) I can't modify any Jackson annotations. Any idea how I can get around this for my unit tests?
Upvotes: 0
Views: 5343
Reputation: 1033
I would say you have a version issue with SDK between tests and production possibly. Looking at their source code for this class:
They appear to have an annotation for jackson on one of the two setters, specifying that's the one to use for serialization / de-serialization. This is the latest version, I didn't tool back in history to see if older version was missing this.
Upvotes: 2