se.bjurr.wiremockpact.wiremockpactextension.support.BaseSetupJunitExtension Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wiremock-pact-extension Show documentation
Show all versions of wiremock-pact-extension Show documentation
Transform Wiremock mappings to PACT.
The newest version!
package se.bjurr.wiremockpact.wiremockpactextension.support;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
import java.util.concurrent.locks.ReentrantLock;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
/** https://stackoverflow.com/questions/43282798/in-junit-5-how-to-run-code-before-all-tests */
public abstract class BaseSetupJunitExtension
implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
/** Gate keeper to prevent multiple Threads within the same routine */
private static final ReentrantLock LOCK = new ReentrantLock();
@Override
public void beforeAll(final ExtensionContext context) throws Exception {
LOCK.lock();
try {
// We need to use a unique key here, across all usages of this particular extension.
final String uniqueKey = this.getClass().getName();
final Object value = context.getRoot().getStore(GLOBAL).get(uniqueKey);
if (value == null) {
// First test container invocation.
context.getRoot().getStore(GLOBAL).put(uniqueKey, this);
this.setup();
}
} finally {
// free the access
LOCK.unlock();
}
}
/**
* Callback that is invoked exactly once before the start of all test
* containers.
*/
public abstract void setup();
/**
* Callback that is invoked exactly once after the end of all test containers.
* Inherited from {@code CloseableResource}
*/
@Override
public abstract void close() throws Throwable;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy