0

我不知道如何在 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.3junit.jupiter.version5.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,
        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();
    }

它失败了:

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)

我在这里想念什么?我检查了映射值,它们似乎没问题。

4

0 回答 0