关于这个问题,我一直在联系Alternator的作者,他和我一样疑惑。简短的故事:我使用 Alternator 模拟框架编写了针对 DynamoDB 运行的代码的单元测试,当我从 Eclipse 中调用它们时工作正常,但从 maven 调用时失败。
故障源于 AWS 开发工具包本身:
com.amazonaws.AmazonServiceException: [java.lang.Error: property value is null.]
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:679) ~[aws-java-sdk-1.5.5.jar:na]
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:350) ~[aws-java-sdk-1.5.5.jar:na]
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:202) ~[aws-java-sdk-1.5.5.jar:na]
at com.michelboudreau.alternatorv2.AlternatorDBClientV2.invoke(AlternatorDBClientV2.java:225) ~[alternator-0.6.4.jar:na]
at com.michelboudreau.alternatorv2.AlternatorDBClientV2.updateItem(AlternatorDBClientV2.java:99) ~[alternator-0.6.4.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:646) ~[aws-java-sdk-1.5.5.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:767) ~[aws-java-sdk-1.5.5.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:658) ~[aws-java-sdk-1.5.5.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:488) ~[aws-java-sdk-1.5.5.jar:na]
at com.somoglobal.eventdata.Controller.addDeviceKeys(Controller.java:531) ~[classes/:na]
正如您所看到的,这并不是很有用,因为堆栈跟踪本身只是在 AWS 开发工具包从通过(模拟)远程服务接收到的响应中组装异常时显示堆栈。
相关版本号:
- Maven 3.0.4
- Java 1.7.0_10
- AWS SDK 1.5.5(虽然我也尝试过 1.5.3 和 1.5.4)
- Eclipse 版本“Kepler”,内部版本号:20130614-0229
再深入一点,项目的(略微省略)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>snip</groupId>
<artifactId>snip</artifactId>
<version>1.14.2-SNAPSHOT</version>
</parent>
<artifactId>snip</artifactId>
<version>1.6.0-SNAPSHOT</version>
<name>snip</name>
<dependencies>
...snip...
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>com.michelboudreau</groupId>
<artifactId>alternator</artifactId>
<version>0.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
和略微修剪的测试:
package com.xxx;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.joda.time.DateTime;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import com.amazonaws.Request;
import com.amazonaws.handlers.RequestHandler;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputExceededException;
import com.amazonaws.util.TimingInfo;
import com.michelboudreau.alternator.AlternatorDB;
import com.michelboudreau.alternatorv2.AlternatorDBClientV2;
import com.xxx.stuff;
public class ControllerTest {
private static Controller instance;
private static DynamoDBMapper mapper;
private static AlternatorDB db;
private static AlternatorDBClientV2 client;
@BeforeClass
public static void setup() throws Exception {
client = new AlternatorDBClientV2();
mapper = new DynamoDBMapper(client);
db = new AlternatorDB().start();
// code to create dynamodb was here
instance = new Controller(mapper);
}
@AfterClass
public static void tearDown() throws Exception {
db.stop();
}
@Test
public void testAddDeviceKeys() {
Collection<DeviceKey> keys = EventDataModelMapper.getDeviceKeys(ClassFixtures.event);
assertNotNull("keys should not be null", keys);
assertFalse("keys should not be empty", keys.isEmpty());
boolean result = instance.addDeviceKeys(keys);
assertTrue("result should be true", result);
}
}
被测代码可能并没有特别涉及此故障 - 我已经完成了足够的调试跟踪,以查看它在测试期间直接通过 Eclipse 调用和从 maven 运行时的行为相同。
编辑
实际上,交流发电机可能与此有关,因为有问题的错误消息可能来自 com.michelboudreau.alternator.validation.ValidatorUtils:
public static <T> List<Error> rejectIfNull(T property) {
List<Error> errors = new ArrayList<Error>();
if (property == null) {
errors.add(new Error("property value is null."));
}
return errors;
}