杰克逊:JUnit测试中的“冲突的属性定位器定义”

时间:2017-09-20 15:30:59

标签: json eclipse junit jackson aws-java-sdk

我有一个使用亚马逊AWS SDK for Java查询EC2 Jackson数据的应用程序。使用AWS SDK中包含的Jackson.fromJsonString(String, Class)包装类将实例序列化为JSON字符串。稍后,我可以使用Instance方法将JSON字符串反序列化为EC2 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 对象。

这一切都适用于我的应用程序代码。但是,从Eclipse中的JUnit测试运行时,它每次都会失败。我正在使用完全相同的数据,以及完全相同的反序列化代码。但是,当我从JUnit测试运行它时,我得到以下异常:

Instance

知道这里究竟出了什么问题吗?为什么它在主应用程序代码中运行得很好,但在作为JUnit测试运行时失败了?由于我不拥有institutes Table id | institute_name | phone_number ---------------------------------------------- 1 | vepsun | 85214542462 ---------------------------------------------- 2 | infocampus | 52466475544 Locations table id | institute_id(fk) | location_name ------------------------------------------------ 1 | 1 | Banglore ------------------------------------------------ 2 | 1 | delhi courses table id | institute_id(fk) | course_name ------------------------------------------------ 1 | 1 | php ------------------------------------------------ 2 | 1 | delhi 对象(它是亚马逊SDK的一部分),我无法修改任何杰克逊注释。我知道如何为我的单元测试解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我想说你可能在测试和生产之间存在SDK的版本问题。查看这个类的源代码:

https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-ec2/src/main/java/com/amazonaws/services/ec2/model/Instance.java

它们似乎在两个setter之一上有jackson的注释,指定用于序列化/反序列化的那个。这是最新版本,我没有在历史记录中找回旧版本是否缺少此版本。