I can't figure out how to use Testcontainers' Dynalite module in a JUnit 5 test to be able to test Amazon DynamoDB, - documentation is really too succinct and minimalist.
So I added the following dependencies in 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>
where testcontainers.version
is 1.14.3
and junit.jupiter.version
is 5.6.2
.
Then I created a service test class as follows:
@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());
}
}
When running the tests, it works, yay!
But when I try to create an entry and test the service as follows:
@Test
void findDailyPowerConsumptions() throws SQLException {
//assertTrue(dynaliteContainer.isRunning());
DailyPowerConsumption dailyPowerConsumption = new DailyPowerConsumption(1, 10, "2020-08-10.000", "DEV1", 10.0,
10.0, 220.0, 10.0, 1.0,1.0, 1.0);
repository.save(dailyPowerConsumption);
List<DailyPowerConsumption> dailyPowerConsumptionsList = dailyPowerConsumptionService.findDailyPowerConsumptions("EX1", "1", "10");
assertThat(dailyPowerConsumptionsList).isNotEmpty();
}
it fails with:
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)
What am I missing here? I checked the mapped values, they seem to be OK.