13

I am getting the following exception when trying to instantiate the LocalstackContainer.I do have this class available and was able to find the source code as well in my local. I am not able to find the root cause. I am using windows os with java 11.

import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;


@TestContainers
public class S3Test {
     private S3Client s3Client() {
            LocalStackContainer localstack= new LocalStackContainer().withServices(LocalStackContainer.Service.S3);
            return S3Client
                    .builder()
                    .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
                    .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
                            localstack.getAccessKey(), localstack.getSecretKey())))
                    .region(Region.of(localstack.getRegion()))
                    .build();
        }

@Test
 public void test() {   
}

}

Error:

java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials   
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at 
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachMethods$2(TestMethodTestDescriptor.java:169)     
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentials
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 71 more
mark
  • 407
  • 7
  • 23
  • Any answer to the above question? – Krishna Kumar Singh Oct 12 '20 at 09:42
  • Yes. As you see in the error log, I was using the wrong package for class om.amazonaws.auth.AWSCredentials. I switched to the AWS SDK V2 class for the same. The error is gone. But eventually, I ended up not using this as there is some issue with PC – mark Oct 12 '20 at 19:39
  • software.amazon.awssdk.auth.credentials.AwsBasicCredentials; This belongs to AWS SDK 2 right? I am getting error in this line public LocalStackContainer localstack = new LocalStackContainer().withServices(LocalStackContainer.Service.S3); – Krishna Kumar Singh Oct 13 '20 at 05:50
  • that's correct. what error you are getting at that line? – mark Oct 13 '20 at 14:48
  • ava.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentials at com.xxxx.TestMainFunctionOfAllParsers.(TestMainFunctionOfAllParsers.java:45) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at – Krishna Kumar Singh Oct 21 '20 at 12:04
  • which version of aws sdk you are using? – mark Oct 21 '20 at 16:14
  • ok. I think AWSCredentials is not available in version 2.x. Instead try with these import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentials; – mark Oct 22 '20 at 23:25
  • I am writing unit test cases. So, I am trying to mock S3client using localstack. Is it possible to mock it like this:s3Client = S3Client .builder() .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3)) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( localstack.getAccessKey(), localstack.getSecretKey() ))) .region(Region.of(localstack.getRegion())) .build(); without using docker. – Krishna Kumar Singh Oct 23 '20 at 08:59
  • 2
    Getting exception in this line: @Rule public LocalStackContainer localstack = new LocalStackContainer().withServices(LocalStackContainer.Service.S3); – Krishna Kumar Singh Oct 23 '20 at 09:00

1 Answers1

32

Although Testcontainers LocalStack supports AWS SDK version 2, it still uses AWS SDK version 1 under the covers, but omits the necessary runtime dependency. So you'll need to include the dependency explicitly:

Maven

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.914</version>
    <scope>test</scope>
</dependency>

Gradle

testImplementation "com.amazonaws:aws-java-sdk-s3:1.11.914"
hertzsprung
  • 9,445
  • 4
  • 42
  • 77