Giter Club home page Giter Club logo

junit5-dynamodb-local-extension's Introduction

JUnit Jupiter DynamoDB Local Extension

Table of Contents

Latest Release Version CircleCI

Note
This documentation is for the HEAD of the repository. To see documentation at a specific version see the GitHub Releases page

Setup

Add as a dependency
repositories {
  jcenter()
  // You will also need a repository to retrieve the DynamoDB Local transitive dependencies.
  // These are listed on https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
  maven {
    description = "Repository from US West 2 - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html"
    name = "dynamodb-local-oregon"
    url = uri("https://s3-us-west-2.amazonaws.com/dynamodb-local/release")
  }
}

dependencies {
  testImplementation("com.mkobit.junit.jupiter.aws:junit5-dynamodb-local-extension:x.x.x")
}

Usage

Using in tests

Note
The extension modifies the sqlite4java.library.path JVM system property. An explanation for why can be found on this Stack Overflow question and answers.

The extension will create a new embedded DynamoDB instance for each test. It attempts to locate the native dependencies on the classpath, place them into a temporary directory, and then set a system property before creating the database for test injection.

Annotate your test class or methods with @DynamoDBLocal and have one of the supported types automatically injected for use in your test. It will be torn down afterwards.

Example showing different types that can be injected.
import com.mkobit.junit.jupiter.aws.dynamodb.DynamoDBLocal;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams
import com.amazonaws.services.dynamodbv2.local.shared.access.AmazonDynamoDBLocal

@DynamoDBLocal
class MyDynamoDbTest {
  @Test
  void usingAmazonDynamoDBLocal(final AmazonDynamoDBLocal amazonDynamoDBLocal) {
  }

  @Test
  void usingAmazonDynamoDB(final AmazonDynamoDB amazonDynamoDB) {
  }

  @Test
  void usingAmazonDynamoDBStreams(final AmazonDynamoDBStreams amazonDynamoDBStreams) {
  }
}

junit5-dynamodb-local-extension's People

Contributors

mkobit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

antonfagerberg

junit5-dynamodb-local-extension's Issues

Clean-up fails on Windows due to locked DLL

There seems to be an issue in the clean-up process on Windows machines. I've been running on MacOS without any problems but the same code fails for my team members running Windows.

I've been able to reproduces this issue in a virtual machine (I used this Windows 10 VirtualBox Image) and the Corretto 8 JDK for Windows x64.

What seems to happen is that the tests run fine, but lastly the temp file clean-up is initiated here:

public void afterAll(final ExtensionContext context) throws Exception {
final ExtensionContext.Store store = getStore(context);
final Path tempNativeLibraries = store.remove(context, Path.class);
if (tempNativeLibraries != null) {
LOGGER.fine(() -> "Deleting all native library files at " + tempNativeLibraries);
Files.walk(tempNativeLibraries)
.sorted(Comparator.reverseOrder())
.peek(path -> LOGGER.fine(() ->"Deleting file/directory located at " + path))
.forEach(ThrowingConsumer.wrap(Files::delete));
}
}

First file is deleted ok: C:\Users\User\AppData\Local\Temp\native-libraries1602891640003380154\sqlite4java-win32-x86-1.0.392.dll

Second file fails: C:\Users\User\AppData\Local\Temp\native-libraries1602891640003380154\sqlite4java-win32-x64-1.0.392.dll presumably because the DLL was loaded.

Stack trace:

java.lang.RuntimeException: Error processing consumer

	at com.mkobit.junit.jupiter.aws.dynamodb.EmbeddedDynamoDBExtension$ThrowingConsumer.lambda$wrap$0(EmbeddedDynamoDBExtension.java:142)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.util.stream.ReferencePipeline$11$1.accept(ReferencePipeline.java:440)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
	at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
	at com.mkobit.junit.jupiter.aws.dynamodb.EmbeddedDynamoDBExtension.afterAll(EmbeddedDynamoDBExtension.java:72)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeAfterAllCallbacks$11(ClassTestDescriptor.java:396)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeAfterAllCallbacks$12(ClassTestDescriptor.java:396)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeAfterAllCallbacks(ClassTestDescriptor.java:396)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.after(ClassTestDescriptor.java:221)
	at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.after(ClassTestDescriptor.java:74)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:119)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:119)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	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:220)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.nio.file.AccessDeniedException: C:\Users\User\AppData\Local\Temp\native-libraries1602891640003380154\sqlite4java-win32-x64-1.0.392.dll
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
	at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
	at java.nio.file.Files.delete(Files.java:1126)
	at com.mkobit.junit.jupiter.aws.dynamodb.EmbeddedDynamoDBExtension$ThrowingConsumer.lambda$wrap$0(EmbeddedDynamoDBExtension.java:140)
	... 41 more

This seems to be caused due to the file being locked by the Java Runtime at this point. If I set a break-point before the deletion and try to delete the file manually with the Windows Explorer it says The action can't be completed because the file is open in OpenJDK Platform binary.

I'm not familiar with Windows myself but trying to delete a DLL which is loaded by the JVM seems like a messy problem:

Since this project isn't responsible for loading the DLL itself and thus (I presume) will have a hard time trying to control the life cycle, perhaps just leaving the files could be a solution? The temp directory should be wiped eventually by the OS anyway and the files are small.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.