Localstack java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials

Localstack java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials

我想在 Spring 使用本地 docker 容器启动时使用 localstack (SQS) 制作一些睾丸,所以我使用 LocalStackContainer 与本地 docker 容器,但是当我 运行 测试时,会发生奇怪的异常。首先,我将展示代码,其次展示堆栈跟踪。

代码:这里我尝试使用 localstack v 0.11.6 和 SQS 作为服务来连接到本地 docker。在这一行中,我遇到了异常。

import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

@SpringBootTest(classes = SqsConsumer.class)
public class SqsConsumerTest {

    public static LocalStackContainer localStackContainer = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.6"))
            .withServices(LocalStackContainer.Service.SQS);

    @BeforeClass
    public static void initLocalStack() throws IOException, InterruptedException {
        localStackContainer.start();

        localStackContainer.execInContainer("awslocal", "sqs", "create-queue", "--queue-name", "image-sqs");
    }

}

Exception/Stacktrace:

java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials

at br.com.imageprocessor.SqsConsumerTest.<clinit>(SqsConsumerTest.java:27)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 18 more

嗯,依赖项:

<dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sqs</artifactId>
    </dependency>

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sso</artifactId>
    </dependency>

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>s3</artifactId>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>localstack</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers</artifactId>
        <scope>test</scope>
    </dependency>

我已经检查了 运行ning docker 容器,我认为那里一切正常。这个问题有点奇怪,我检查了依赖关系,它可能是问题所在,但我没有找到任何有效的依赖关系。

编辑: 使用来自 maven 的另一个依赖项后,它起作用了。谢谢大家

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-sqs</artifactId>
    <version>1.12.142</version>
</dependency>

尝试添加以下依赖项:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-sqs</artifactId>
    <version>1.12.142</version>
</dependency>

可能与这个问题有关:JUnit AWSCredentials NoClassDefFoundError with LocalStackContainer

编辑:将工件 ID 从 aws-java-sdk-s3

更改为 aws-java-sdk-sqs