Java 更轻松的 DynamoDB 本地测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26901613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:29:12  来源:igfitidea点击:

Easier DynamoDB local testing

javaunit-testingjunitamazon-dynamodb

提问by Oliver Dain

I'm using DynamoDB localfor unit testing. It's not bad, but has some drawbacks. Specifically:

我在本地使用DynamoDB进行单元测试。这还不错,但有一些缺点。具体来说:

  • You have to somehow start the server before your tests run
  • The server isn't started and stopped before each test so tests become inter-dependent unless you add code to delete all tables, etc. after each test
  • All developers need to have it installed
  • 您必须在测试运行之前以某种方式启动服务器
  • 服务器不会在每次测试之前启动和停止,因此测试变得相互依赖,除非您在每次测试后添加代码以删除所有表等
  • 所有开发人员都需要安装它

What I want to do is something like put the DynamoDB local jar, and the other jars upon which it depends, in my test/resourcesdirectory (I'm writing in Java). Then before each test I'd start it up, running with -inMemory, and after the test I'd stop it. That way anyone pulling down the git repo gets a copy of everything they need to run the tests and each test is independent of the others.

我想要做的是将 DynamoDB 本地 jar 以及它所依赖的其他 jar 放在我的test/resources目录中(我正在用 Java 编写)。然后在每次测试之前我会启动它,运行-inMemory,并在测试之后我会停止它。这样,任何拉下 git repo 的人都会获得运行测试所需的所有内容的副本,并且每个测试都独立于其他测试。

I have found a way to make this work, but it's ugly, so I'm looking for alternatives. The solution I have is to put a .zip file of the DynamoDB local stuff in test/resources, then in the @Beforemethod, I'd extract it to some temporary directory and start a new java process to execute it. That works, but it's ugly and has some drawbacks:

我找到了一种方法来完成这项工作,但它很难看,所以我正在寻找替代方案。该解决方案我已经是放的DynamoDB本地的东西的.zip文件中test/resources,然后在@Before方法,我将其解压到某个临时目录,并开始一个新的Java程序来执行它。这有效,但它很丑陋并且有一些缺点:

  • Everyone needs the java executable on their $PATH
  • I have to unpack a zip to the local disk. Using local disk is often dicey for testing, especially with continuous builds and such.
  • I have to spawn a process and wait for it to start for each unit test, and then kill that process after each test. Besides being slow, the potential for left-over processes seems ugly.
  • 每个人都需要他们的 java 可执行文件 $PATH
  • 我必须将 zip 解压缩到本地磁盘。使用本地磁盘进行测试通常很冒险,尤其是在连续构建等情况下。
  • 我必须生成一个进程并等待它为每个单元测试启动,然后在每次测试后终止该进程。除了缓慢之外,剩余过程的潜力似乎很丑陋。

It seems like there should be an easier way. DynamoDB Local is, after all, just Java code. Can't I somehow ask the JVM to fork itself and look inside the resources to build a classpath? Or, even better, can't I just call the mainmethod of DynamoDB Local from some other thread so this all happens in a single process? Any ideas?

似乎应该有更简单的方法。毕竟,DynamoDB Local 只是 Java 代码。我不能以某种方式要求 JVM 分叉自己并查看资源内部以构建类路径吗?或者,更好的是,我不能只main从其他线程调用DynamoDB Local的方法,这样这一切都发生在一个进程中吗?有任何想法吗?

PS: I am aware of Alternator, but it appears to have other drawbacks so I'm inclined to stick with Amazon's supported solution if I can make it work.

PS:我知道 Alternator,但它似乎还有其他缺点,所以如果我可以让它工作,我倾向于坚持使用亚马逊支持的解决方案。

回答by Steve Smith

For unit testing at work I use Mockito, then just mock the AmazonDynamoDBClient. then mock out the returns using when. like the following:

对于工作中的单元测试,我使用 Mockito,然后只模拟 AmazonDynamoDBClient。然后使用 when 模拟返回值。像下面这样:

when(mockAmazonDynamoDBClient.getItem(isA(GetItemRequest.class))).thenAnswer(new Answer<GetItemResult>() {
        @Override
        public GetItemResult answer(InvocationOnMock invocation) throws Throwable {
            GetItemResult result = new GetItemResult();
            result.setItem( testResultItem );
            return result;
        }
    });

not sure if that is what your looking for but that's how we do it.

不确定这是否是您要找的,但我们就是这样做的。

回答by Alexander Patrikalakis

You can use DynamoDB Local as a Maven test dependency in your test code, as is shown in this announcement. You can run over HTTP:

您可以在测试代码中使用 DynamoDB Local 作为 Maven 测试依赖项,如本公告中所示。您可以通过 HTTP 运行:

import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;

final String[] localArgs = { "-inMemory" };
DynamoDBProxyServer server = ServerRunner.createServerFromCommandLineArgs(localArgs);
server.start();
AmazonDynamoDB dynamodb = new AmazonDynamoDBClient();
dynamodb.setEndpoint("http://localhost:8000");
dynamodb.listTables();
server.stop();

You can also run in embedded mode:

您还可以在嵌入式模式下运行:

import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;

AmazonDynamoDB dynamodb = DynamoDBEmbedded.create();
dynamodb.listTables();

回答by bhdrkn

In order to use DynamoDBLocal you need to follow these steps.

要使用 DynamoDBLocal,您需要执行以下步骤。

  1. Get Direct DynamoDBLocal Dependency
  2. Get Native SQLite4Java dependencies
  3. Set sqlite4java.library.pathto show native libraries
  1. 获取直接的 DynamoDBLocal 依赖
  2. 获取本机 SQLite4Java 依赖项
  3. 设置sqlite4java.library.path为显示本机库

1. Get Direct DynamoDBLocal Dependency

1. 获取直接的 DynamoDBLocal 依赖

This one is the easy one. You need this repository as explained in AWS Forums.

这是最简单的一种。您需要此存储库,如AWS 论坛 中所述

<!--Dependency:-->
<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>DynamoDBLocal</artifactId>
        <version>1.11.0.1</version>
        <scope></scope>
    </dependency>
</dependencies>
<!--Custom repository:-->
<repositories>
    <repository>
        <id>dynamodb-local</id>
        <name>DynamoDB Local Release Repository</name>
        <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
    </repository>
</repositories>

2. Get Native SQLite4Java dependencies

2. 获取 Native SQLite4Java 依赖

If you do not add these dependencies, your tests will fail with 500 internal error.

如果您不添加这些依赖项,您的测试将失败并显示 500 内部错误。

First, add these dependencies:

首先,添加这些依赖项:

<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>sqlite4java</artifactId>
    <version>1.0.392</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>sqlite4java-win32-x86</artifactId>
    <version>1.0.392</version>
    <type>dll</type>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>sqlite4java-win32-x64</artifactId>
    <version>1.0.392</version>
    <type>dll</type>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>libsqlite4java-osx</artifactId>
    <version>1.0.392</version>
    <type>dylib</type>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>libsqlite4java-linux-i386</artifactId>
    <version>1.0.392</version>
    <type>so</type>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.almworks.sqlite4java</groupId>
    <artifactId>libsqlite4java-linux-amd64</artifactId>
    <version>1.0.392</version>
    <type>so</type>
    <scope>test</scope>
</dependency>

Then, add this plugin to get native dependencies to specific folder:

然后,添加此插件以获取特定文件夹的本机依赖项:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>test</includeScope>
                        <includeTypes>so,dll,dylib</includeTypes>
                        <outputDirectory>${project.basedir}/native-libs</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. Set sqlite4java.library.pathto show native libraries

3.设置sqlite4java.library.path显示本机库

As last step, you need to set sqlite4java.library.pathsystem property to native-libs directory. It is OK to do that just before creating your local server.

最后一步,您需要将sqlite4java.library.path系统属性设置为 native-libs 目录。在创建本地服务器之前这样做是可以的。

System.setProperty("sqlite4java.library.path", "native-libs");

After these steps you can use DynamoDBLocal as you want. Here is a Junit rule that creates local server for that.

完成这些步骤后,您可以根据需要使用 DynamoDBLocal。这是为此创建本地服务器的 Junit 规则。

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import org.junit.rules.ExternalResource;

import java.io.IOException;
import java.net.ServerSocket;

/**
 * Creates a local DynamoDB instance for testing.
 */
public class LocalDynamoDBCreationRule extends ExternalResource {

    private DynamoDBProxyServer server;
    private AmazonDynamoDB amazonDynamoDB;

    public LocalDynamoDBCreationRule() {
        // This one should be copied during test-compile time. If project's basedir does not contains a folder
        // named 'native-libs' please try '$ mvn clean install' from command line first
        System.setProperty("sqlite4java.library.path", "native-libs");
    }

    @Override
    protected void before() throws Throwable {

        try {
            final String port = getAvailablePort();
            this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
            server.start();
            amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials("access", "secret"));
            amazonDynamoDB.setEndpoint("http://localhost:" + port);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void after() {

        if (server == null) {
            return;
        }

        try {
            server.stop();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public AmazonDynamoDB getAmazonDynamoDB() {
        return amazonDynamoDB;
    }

    private String getAvailablePort() {
        try (final ServerSocket serverSocket = new ServerSocket(0)) {
            return String.valueOf(serverSocket.getLocalPort());
        } catch (IOException e) {
            throw new RuntimeException("Available port was not found", e);
        }
    }
}

You can use this rule like this

你可以像这样使用这个规则

@RunWith(JUnit4.class)
public class UserDAOImplTest {

    @ClassRule
    public static final LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule();
}

回答by Ashan

There are couple of node.js wrappers for DynamoDB Local. These allows to easily execute unit tests combining with task runners like gulp or grunt. Try dynamodb-localhost, dynamodb-local

DynamoDB Local 有几个 node.js 包装器。这些允许与任务运行器(如 gulp 或 grunt)结合轻松执行单元测试。尝试dynamodb-localhost, dynamodb-local

回答by Jeffery Grajkowski

This is a restating of bhdrkn's answer for Gradle users (his is based on Maven). It's still the same three steps:

这是 bhdrkn 对 Gradle 用户的回答的重述(他的回答基于 Maven)。还是一样的三步:

  1. Get Direct DynamoDBLocal Dependency
  2. Get Native SQLite4Java dependencies
  3. Set sqlite4java.library.path to show native libraries
  1. 获取直接的 DynamoDBLocal 依赖
  2. 获取本机 SQLite4Java 依赖项
  3. 设置 sqlite4java.library.path 以显示本机库

1. Get Direct DynamoDBLocal Dependency

1. 获取直接的 DynamoDBLocal 依赖

Add to the dependencies section of your build.gradle file...

添加到 build.gradle 文件的依赖项部分...

dependencies {
    testCompile "com.amazonaws:DynamoDBLocal:1.+"
}

2. Get Native SQLite4Java dependencies

2. 获取 Native SQLite4Java 依赖

The sqlite4java libraries will already be downloaded as a dependency of DynamoDBLocal, but the library files need to be copied to the right place. Add to your build.gradle file...

sqlite4java 库已作为 DynamoDBLocal 的依赖项下载,但需要将库文件复制到正确的位置。添加到您的 build.gradle 文件...

task copyNativeDeps(type: Copy) {
    from(configurations.compile + configurations.testCompile) {
        include '*.dll'
        include '*.dylib'
        include '*.so'
    }
    into 'build/libs'
}

3. Set sqlite4java.library.path to show native libraries

3.设置sqlite4java.library.path显示原生库

We need to tell Gradle to run copyNativeDepsfor testing and tell sqlite4java where to find the files. Add to your build.gradle file...

我们需要告诉 Gradle 运行copyNativeDeps测试并告诉 sqlite4java 在哪里可以找到文件。添加到您的 build.gradle 文件...

test {
    dependsOn copyNativeDeps
    systemProperty "java.library.path", 'build/libs'
}

回答by Michael Lloyd Lee mlk

I have wrapped the answers above into two JUnit rulesthat does not require changes to the build script as the rules handles the native library stuff. I did this as I found that Idea did not like the Gradle/Maven solutions as it just went off and did its own thing anyhoos.

我将上面的答案包装到两个JUnit 规则中,这些规则不需要更改构建脚本,因为这些规则处理本机库的内容。我这样做是因为我发现 Idea 不喜欢 Gradle/Maven 解决方案,因为它刚刚开始并做自己的事情。

This means the steps are:

这意味着步骤是:

  • Get the AssortmentOfJUnitRules version 1.5.32 or above dependency
  • Get the Direct DynamoDBLocal dependency
  • Add the LocalDynamoDbRule or HttpDynamoDbRule to your JUnit test.
  • 获取 AssortmentOfJUnitRules 1.5.32 或以上版本的依赖
  • 获取直接 DynamoDBLocal 依赖项
  • 将 LocalDynamoDbRule 或 HttpDynamoDbRule 添加到您的 JUnit 测试中。

Maven:

马文:

<!--Dependency:-->
<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>DynamoDBLocal</artifactId>
        <version>1.11.0.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.github.mlk</groupId>
      <artifactId>assortmentofjunitrules</artifactId>
      <version>1.5.36</version>
      <scope>test</scope>
    </dependency>
</dependencies>
<!--Custom repository:-->
<repositories>
    <repository>
        <id>dynamodb-local</id>
        <name>DynamoDB Local Release Repository</name>
        <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
    </repository>
</repositories>

Gradle:

摇篮:

repositories {
  mavenCentral()

   maven {
    url = "https://s3-us-west-2.amazonaws.com/dynamodb-local/release"
  }
}

dependencies {
    testCompile "com.github.mlk:assortmentofjunitrules:1.5.36"
    testCompile "com.amazonaws:DynamoDBLocal:1.+"
}

Code:

代码:

public class LocalDynamoDbRuleTest {
  @Rule
  public LocalDynamoDbRule ddb = new LocalDynamoDbRule();

  @Test
  public void test() {
    doDynamoStuff(ddb.getClient());
  }
}

回答by Michael Coxon

I have found that the amazon repo as no index file, so does not seem to function in a way that allows you to bring it in like this:

我发现 amazon repo 没有索引文件,因此似乎无法以允许您将其引入的方式运行:

maven {
   url = "https://s3-us-west-2.amazonaws.com/dynamodb-local/release"
}

The only way I could get the dependencies to load is by downloading DynamoDbLocal as a jar and bringing it into my build script like this:

我可以加载依赖项的唯一方法是将 DynamoDbLocal 作为 jar 下载并将其放入我的构建脚本中,如下所示:

dependencies {
    ...
    runtime files('libs/DynamoDBLocal.jar')
    ...
}

Of course this means that all the SQLite and Jetty dependencies need to be brought in by hand - I'm still trying to get this right. If anyone knows of a reliable repo for DynamoDbLocal, I would really love to know.

当然,这意味着所有 SQLite 和 Jetty 依赖项都需要手动引入 - 我仍在努力解决这个问题。如果有人知道 DynamoDbLocal 的可靠存储库,我真的很想知道。

回答by madhead

In August 2018 Amazon announcednew Docker imagewith Amazon DynamoDB Local onboard. It does not require downloading and running any JARs as well as adding using third-party OS-specific binaries (I'm talking about sqlite4java).

2018 年 8 月,亚马逊发布了带有 Amazon DynamoDB Local 的新Docker 镜像。它不需要下载和运行任何 JAR,也不需要使用第三方操作系统特定的二进制文件(我说的是sqlite4java)。

It is as simple as starting a Docker container before the tests:

它就像在测试之前启动一个 Docker 容器一样简单:

docker run -p 8000:8000 amazon/dynamodb-local

You can do that manually for local development, as described above, or use it in your CI pipeline. Many CI services provide an ability to start additional containers during the pipeline that can provide dependencies for your tests. Here is an example for Gitlab CI/CD:

如上所述,您可以为本地开发手动执行此操作,也可以在 CI 管道中使用它。许多 CI 服务提供在管道期间启动额外容器的能力,这些容器可以为您的测试提供依赖项。以下是 Gitlab CI/CD 的示例:

test:
  stage: test
  image: openjdk:8-alpine
  services:
    - name: amazon/dynamodb-local
      alias: dynamodb-local
  script:
    - DYNAMODB_LOCAL_URL=http://dynamodb-local:8000 ./gradlew clean test

Or Bitbucket Pipelines:

或 Bitbucket 管道:

definitions:
  services:
    dynamodb-local:
      image: amazon/dynamodb-local
…
step:
  name: test
  image:
    name: openjdk:8-alpine
  services:
    - dynamodb-local
  script:
    - DYNAMODB_LOCAL_URL=http://localhost:8000 ./gradlew clean test

And so on. The idea is to move all the configuration you can see in otheranswersout of your build tool and provide the dependency externally. Think of it as of dependency injection / IoC but for the whole service, not just a single bean.

等等。这个想法是将您在其他答案中可以看到的所有配置移出构建工具,并在外部提供依赖项。将其视为依赖注入/IoC,但对于整个服务,而不仅仅是单个 bean。

After you've started the container you can create a client pointing to it:

启动容器后,您可以创建一个指向它的客户端:

private AmazonDynamoDB createAmazonDynamoDB(final DynamoDBLocal configuration) {
    return AmazonDynamoDBClientBuilder
        .standard()
        .withEndpointConfiguration(
            new AwsClientBuilder.EndpointConfiguration(
                "http://localhost:8000",
                Regions.US_EAST_1.getName()
            )
        )
        .withCredentials(
            new AWSStaticCredentialsProvider(
                // DynamoDB Local works with any non-null credentials
                new BasicAWSCredentials("", "")
            )
        )
        .build();
}

Now to the original questions:

现在回到最初的问题:

You have to somehow start the server before your tests run

您必须在测试运行之前以某种方式启动服务器

You can just start it manually, or prepare a developsers' script for it. IDEs usually provide a way to run arbitrary commands before executing a task, so you can make IDEto start the container for you. I think that running something locally should not be a top priority in this case, but instead you should focus on configuring CI and let the developers start the container as it's comfortable to them.

您可以手动启动它,或者为它准备一个开发人员的脚本。IDE 通常提供一种在执行任务之前运行任意命令的方法,因此您可以让 IDE为您启动容器。我认为在这种情况下,在本地运行某些东西不应该是重中之重,相反,您应该专注于配置 CI,让开发人员启动容器,因为这对他们来说很舒服。

The server isn't started and stopped before each test so tests become inter-dependent unless you add code to delete all tables, etc. after each test

服务器不会在每次测试之前启动和停止,因此测试变得相互依赖,除非您在每次测试后添加代码以删除所有表等

That's trueee, but… You should not start and stop such heavyweight things and recreate tables before / after each test. DB tests are almost always inter-dependent and that's ok for them. Just use unique values for each test case (e.g. set item's hash key to ticket id / specific test case id you're working on). As for the seed data, I'd recommend moving it from the build tool and test code as well. Either make your own image with all the data you need or use AWS CLI to create tables and insert data. Follow the single responsibility principle and dependency injection principles: your test code must not do anything but tests. All the environment (tables and data in this case should be provided for them). Creating a table in a test is wrong, because in a real life that table already exist (unless you're testing a method that actually creates a table, of course).

没错,但是……您不应该在每次测试之前/之后启动和停止这些重量级的事情并重新创建表。数据库测试几乎总是相互依赖的,这对他们来说没问题。只需为每个测试用例使用唯一的值(例如,将项目的哈希键设置为您正在处理的票证 ID/特定测试用例 ID)。至于种子数据,我建议也将其从构建工具和测试代码中移出。要么使用您需要的所有数据制作您自己的图像,要么使用 AWS CLI 创建表并插入数据。遵循单一职责原则和依赖注入原则:你的测试代码除了测试什么都不做。所有环境(在这种情况下应为它们提供表和数据)。在测试中创建表是错误的,因为在现实生活中该表已经存在(除非您

All developers need to have it installed

所有开发人员都需要安装它

Docker should be a must for every developer in 2018, so that's not a problem.

Docker 应该是 2018 年每个开发者的必备品,所以这不是问题。



And if you're using JUnit 5, it can be a good idea to use a DynamoDB Local extensionthat will inject the client in your tests (yes, I'm doing a self-promotion):

如果您使用的是 JUnit 5,最好使用DynamoDB Local 扩展来在您的测试中注入客户端(是的,我正在进行自我宣传):

  1. Add JCenterrepository to your build.

    pom.xml:

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    

    build.gradle

    repositories {
        jcenter()
    }
    
  2. Add a dependency on by.dev.madhead.aws-junit5:dynamodb-v1

    pom.xml:

    <dependency>
        <groupId>by.dev.madhead.aws-junit5</groupId>
        <artifactId>dynamodb-v1</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
    

    build.gradle

    dependencies {
        testImplementation("by.dev.madhead.aws-junit5:dynamodb-v1:1.0.0")
    }
    
  3. Use the extension in your tests:

    @ExtendWith(DynamoDBLocalExtension.class)
        class MultipleInjectionsTest {
        @DynamoDBLocal(
            url = "http://dynamodb-local-1:8000"
        )
        private AmazonDynamoDB first;
    
        @DynamoDBLocal(
            urlEnvironmentVariable = "DYNAMODB_LOCAL_URL"
        )
        private AmazonDynamoDB second;
    
        @Test
        void test() {
            first.listTables();
            second.listTables();
        }
    }
    
  1. JCenter存储库添加到您的构建中。

    pom.xml:

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    

    构建.gradle

    repositories {
        jcenter()
    }
    
  2. 添加依赖 by.dev.madhead.aws-junit5:dynamodb-v1

    pom.xml:

    <dependency>
        <groupId>by.dev.madhead.aws-junit5</groupId>
        <artifactId>dynamodb-v1</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
    

    构建.gradle

    dependencies {
        testImplementation("by.dev.madhead.aws-junit5:dynamodb-v1:1.0.0")
    }
    
  3. 在您的测试中使用扩展:

    @ExtendWith(DynamoDBLocalExtension.class)
        class MultipleInjectionsTest {
        @DynamoDBLocal(
            url = "http://dynamodb-local-1:8000"
        )
        private AmazonDynamoDB first;
    
        @DynamoDBLocal(
            urlEnvironmentVariable = "DYNAMODB_LOCAL_URL"
        )
        private AmazonDynamoDB second;
    
        @Test
        void test() {
            first.listTables();
            second.listTables();
        }
    }
    

回答by Sandeep Lakdawala

You could also use this lightweight test container 'Dynalite'

你也可以使用这个轻量级的测试容器“Dynalite”

https://www.testcontainers.org/modules/databases/dynalite/

https://www.testcontainers.org/modules/databases/dynalite/

From testcontainers:

从测试容器:

Dynalite is a clone of DynamoDB, enabling local testing. It's light and quick to run.

Dynalite 是 DynamoDB 的克隆,支持本地测试。运行起来轻便快捷。