问题描述
我不知道如何在JUnit 5测试中使用Testcontainers的Dynalite模块来测试Amazon DynamoDB,-文档确实太简洁和极简了。
因此,我在pom.xml
中添加了以下依赖项:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.842</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>dynalite</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
其中testcontainers.version
是1.14.3
,而junit.jupiter.version
是5.6.2
。
然后,我创建了一个服务测试类,如下所示:
@Testcontainers
@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.RANDOM_PORT)
class DailyPowerConsumptionServiceTest {
@Container
public static DynaliteContainer dynaliteContainer = new DynaliteContainer();
@TestConfiguration
static class UserServiceImpltestContextConfiguration {
@Bean
DailyPowerConsumptionService dailyPowerConsumptionService() {
return new DailyPowerConsumptionServiceImpl();
}
}
@Autowired
private DailyPowerConsumptionRepository repository;
@Autowired
private DailyPowerConsumptionService dailyPowerConsumptionService;
@BeforeClass
public void setUp() {
AmazonDynamoDB amazonDynamoDB = dynaliteContainer.getClient();
DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper
.generateCreateTableRequest(DailyPowerConsumption.class);
tableRequest.setProvisionedThroughput(
new ProvisionedThroughput(1L,1L));
amazonDynamoDB.createTable(tableRequest);
dynamoDBMapper.batchDelete(repository.findAll());
}
@Test
void findDailyPowerConsumptions() {
assertTrue(dynaliteContainer.isRunning());
}
}
运行测试时,可以,是的!
但是,当我尝试创建条目并按以下方式测试服务时:
@Test
void findDailyPowerConsumptions() throws sqlException {
//assertTrue(dynaliteContainer.isRunning());
DailyPowerConsumption dailyPowerConsumption = new DailyPowerConsumption(1,10,"2020-08-10.000","DEV1",10.0,220.0,1.0,1.0);
repository.save(dailyPowerConsumption);
List<DailyPowerConsumption> dailyPowerConsumptionsList = dailyPowerConsumptionService.findDailyPowerConsumptions("EX1","1","10");
assertthat(dailyPowerConsumptionsList).isNotEmpty();
}
它失败并显示:
com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 71B6U0QKNDARKLUEBQQ99B6DD7VV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
我在这里想念什么?我检查了映射的值,它们似乎还可以。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)