How to use findSharedContainers method of org.testcontainers.junit.jupiter.TestcontainersExtension class

Best Testcontainers-java code snippet using org.testcontainers.junit.jupiter.TestcontainersExtension.findSharedContainers

Source:TestcontainersExtension.java Github

copy

Full Screen

...44 public void beforeAll(ExtensionContext context) {45 Class<?> testClass = context.getTestClass()46 .orElseThrow(() -> new ExtensionConfigurationException("TestcontainersExtension is only supported for classes."));47 Store store = context.getStore(NAMESPACE);48 List<StoreAdapter> sharedContainersStoreAdapters = findSharedContainers(testClass);49 sharedContainersStoreAdapters.forEach(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()));50 List<TestLifecycleAware> lifecycleAwareContainers = sharedContainersStoreAdapters51 .stream()52 .filter(this::isTestLifecycleAware)53 .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)54 .collect(toList());55 store.put(SHARED_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);56 signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));57 }58 @Override59 public void afterAll(ExtensionContext context) {60 signalAfterTestToContainersFor(SHARED_LIFECYCLE_AWARE_CONTAINERS, context);61 }62 @Override63 public void beforeEach(final ExtensionContext context) {64 Store store = context.getStore(NAMESPACE);65 List<TestLifecycleAware> lifecycleAwareContainers = collectParentTestInstances(context).parallelStream()66 .flatMap(this::findRestartContainers)67 .peek(adapter -> store.getOrComputeIfAbsent(adapter.getKey(), k -> adapter.start()))68 .filter(this::isTestLifecycleAware)69 .map(lifecycleAwareAdapter -> (TestLifecycleAware) lifecycleAwareAdapter.container)70 .collect(toList());71 store.put(LOCAL_LIFECYCLE_AWARE_CONTAINERS, lifecycleAwareContainers);72 signalBeforeTestToContainers(lifecycleAwareContainers, testDescriptionFrom(context));73 }74 @Override75 public void afterEach(ExtensionContext context) {76 signalAfterTestToContainersFor(LOCAL_LIFECYCLE_AWARE_CONTAINERS, context);77 }78 private void signalBeforeTestToContainers(List<TestLifecycleAware> lifecycleAwareContainers, TestDescription testDescription) {79 lifecycleAwareContainers.forEach(container -> container.beforeTest(testDescription));80 }81 private void signalAfterTestToContainersFor(String storeKey, ExtensionContext context) {82 List<TestLifecycleAware> lifecycleAwareContainers =83 (List<TestLifecycleAware>) context.getStore(NAMESPACE).get(storeKey);84 if (lifecycleAwareContainers != null) {85 TestDescription description = testDescriptionFrom(context);86 Optional<Throwable> throwable = context.getExecutionException();87 lifecycleAwareContainers.forEach(container -> container.afterTest(description, throwable));88 }89 }90 private TestDescription testDescriptionFrom(ExtensionContext context) {91 return new TestcontainersTestDescription(92 context.getUniqueId(),93 filesystemFriendlyNameOf(context)94 );95 }96 private boolean isTestLifecycleAware(StoreAdapter adapter) {97 return adapter.container instanceof TestLifecycleAware;98 }99 @Override100 public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {101 return findTestcontainers(context).map(this::evaluate)102 .orElseThrow(() -> new ExtensionConfigurationException("@Testcontainers not found"));103 }104 private Optional<Testcontainers> findTestcontainers(ExtensionContext context) {105 Optional<ExtensionContext> current = Optional.of(context);106 while (current.isPresent()) {107 Optional<Testcontainers> testcontainers = AnnotationUtils.findAnnotation(current.get().getRequiredTestClass(), Testcontainers.class);108 if (testcontainers.isPresent()) {109 return testcontainers;110 }111 current = current.get().getParent();112 }113 return Optional.empty();114 }115 private ConditionEvaluationResult evaluate(Testcontainers testcontainers) {116 if (testcontainers.disabledWithoutDocker()) {117 if (isDockerAvailable()) {118 return ConditionEvaluationResult.enabled("Docker is available");119 }120 return ConditionEvaluationResult.disabled("disabledWithoutDocker is true and Docker is not available");121 }122 return ConditionEvaluationResult.enabled("disabledWithoutDocker is false");123 }124 boolean isDockerAvailable() {125 try {126 DockerClientFactory.instance().client();127 return true;128 } catch (Throwable ex) {129 return false;130 }131 }132 private Set<Object> collectParentTestInstances(final ExtensionContext context) {133 Set<Object> testInstances = new LinkedHashSet<>();134 Optional<ExtensionContext> current = Optional.of(context);135 while (current.isPresent()) {136 ExtensionContext ctx = current.get();137 Object testInstance = ctx.getStore(NAMESPACE).remove(TEST_INSTANCE);138 if (testInstance != null) {139 testInstances.add(testInstance);140 }141 current = ctx.getParent();142 }143 return testInstances;144 }145 private List<StoreAdapter> findSharedContainers(Class<?> testClass) {146 return ReflectionUtils.findFields(147 testClass,148 isSharedContainer(),149 ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)150 .stream()151 .map(f -> getContainerInstance(null, f))152 .collect(toList());153 }154 private Predicate<Field> isSharedContainer() {155 return isContainer().and(ReflectionUtils::isStatic);156 }157 private Stream<StoreAdapter> findRestartContainers(Object testInstance) {158 return ReflectionUtils.findFields(159 testInstance.getClass(),...

Full Screen

Full Screen

findSharedContainers

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.junit.jupiter.api.extension.ExtendWith;3import org.testcontainers.containers.GenericContainer;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6@ExtendWith(TestcontainersExtension.class)7class TestcontainersExtensionTest {8 private final GenericContainer<?> container1 = new GenericContainer<>("alpine:3.9.3")9 .withCommand("tail", "-f", "/dev/null");10 private final GenericContainer<?> container2 = new GenericContainer<>("alpine:3.9.3")11 .withCommand("tail", "-f", "/dev/null");12 void test() {13 System.out.println("Container1: " + container1.getContainerId());14 System.out.println("Container2: " + container2.getContainerId());15 }16}17package com.testcontainers.testcontainersextensiontest;18import org.junit.jupiter.api.Test;19import org.junit.jupiter.api.extension.ExtendWith;20import org.testcontainers.containers.GenericContainer;21import org.testcontainers.junit.jupiter.Container;22import org.testcontainers.junit.jupiter.Testcontainers;23import org.testcontainers.junit.jupiter.extension.TestcontainersExtension;24@ExtendWith(TestcontainersExtension.class)25class SharedContainerTest {26 private final GenericContainer<?> container1 = new GenericContainer<>("alpine:3.9.3")27 .withCommand("tail", "-f", "/dev/null");28 private final GenericContainer<?> container2 = new GenericContainer<>("alpine:3.9.3")29 .withCommand("tail", "-f", "/dev/null");30 void test() {31 System.out.println("Container1: " + container1.getContainerId());

Full Screen

Full Screen

findSharedContainers

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.junit.jupiter.api.extension.ExtendWith;3import org.testcontainers.containers.GenericContainer;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6import java.util.List;7@ExtendWith(TestcontainersExtension.class)8public class SharedContainerTest {9 private GenericContainer<?> container1 = new GenericContainer<>("alpine:3.8");10 private GenericContainer<?> container2 = new GenericContainer<>("alpine:3.8");11 void test1() {12 System.out.println("container1 = " + container1.getContainerId());13 System.out.println("container2 = " + container2.getContainerId());14 }15 void test2() {16 System.out.println("container1 = " + container1.getContainerId());17 System.out.println("container2 = " + container2.getContainerId());18 }19 void test3(TestcontainersExtension extension) {20 List<GenericContainer<?>> sharedContainers = extension.findSharedContainers(container1, container2);21 System.out.println("sharedContainers = " + sharedContainers);22 }23}24sharedContainers = [GenericContainer (image name: alpine:3.8, container state: Started)]

Full Screen

Full Screen

findSharedContainers

Using AI Code Generation

copy

Full Screen

1org.testcontainers.junit.jupiter.TestcontainersExtension.findSharedContainers(java.lang.Class<? extends org.testcontainers.junit.jupiter.TestcontainersExtension>)2org.testcontainers.junit.jupiter.TestcontainersExtension.findSharedContainers(java.lang.Class<? extends org.testcontainers.junit.jupiter.TestcontainersExtension>)3public static <T extends org.testcontainers.junit.jupiter.TestcontainersExtension> java.util.List<T> findSharedContainers(java.lang.Class<T> extensionClass)4public static <T extends org.testcontainers.junit.jupiter.TestcontainersExtension> java.util.List<T> findSharedContainers(java.lang.Class<T> extensionClass)5public static void registerExtension(org.junit.jupiter.api.extension.Extension extension)6public static void registerExtension(org.junit.jupiter.api.extension.Extension extension, boolean shared)7public static void registerExtension(org.junit.jupiter.api.extension.Extension extension, boolean shared, java.util.function.Supplier<org.testcontainers.lifecycle.TestDescription> testDescriptionSupplier)8public static void registerExtension(org.junit.jupiter.api.extension.Extension extension, java.util.function.Supplier<org.testcontainers.lifecycle.TestDescription> testDescriptionSupplier)9public static void unregisterExtension(org.junit.jupiter.api.extension.Extension extension)10public static void unregisterExtension(org.junit.jupiter.api.extension.Extension extension, boolean shared)11public static void unregisterExtension(org.junit.jupiter.api.extension.Extension extension, boolean shared, java.util.function.Supplier<org.testcontainers.lifecycle.TestDescription> testDescriptionSupplier)12public static void unregisterExtension(org.junit.jupiter.api.extension.Extension extension, java.util.function.Supplier<org.testcontainers.lifecycle.TestDescription> testDescriptionSupplier)13public static void registerTestDescriptionProvider(java.util.function.Supplier<org.testcontainers.lifecycle.TestDescription> testDescriptionSupplier)14public static void unregisterTestDescriptionProvider(java.util.function.Supplier<org.testcontainers

Full Screen

Full Screen

findSharedContainers

Using AI Code Generation

copy

Full Screen

1@ExtendWith(TestcontainersExtension.class)2public class SharedContainerTest {3 public static GenericContainer sharedContainer = new GenericContainer("alpine:3.9.3")4 .withCommand("tail", "-f", "/dev/null")5 .withSharedMemorySize(2 * 1024 * 1024L);6 public void testSharedContainer() {7 sharedContainer.execInContainer("sh", "-c", "echo 'Hello World' > /hello");8 sharedContainer.execInContainer("sh", "-c", "cat /hello");9 }10}11@ExtendWith(TestcontainersExtension.class)12public class SharedContainerTest {13 public static GenericContainer sharedContainer = new GenericContainer("alpine:3.9.3")14 .withCommand("tail", "-f", "/dev/null")15 .withSharedMemorySize(2 * 1024 * 1024L);16 public void testSharedContainer() {17 sharedContainer.execInContainer("sh", "-c", "echo 'Hello World' > /hello");18 sharedContainer.execInContainer("sh", "-c", "cat /hello");19 }20}21@ExtendWith(TestcontainersExtension.class)22public class SharedContainerTest {23 public static GenericContainer sharedContainer = new GenericContainer("alpine:3.9.3")24 .withCommand("tail", "-f", "/dev/null")25 .withSharedMemorySize(2 * 1024 * 1024L);26 public void testSharedContainer() {27 sharedContainer.execInContainer("sh", "-c", "echo 'Hello World' > /hello");28 sharedContainer.execInContainer("sh", "-c", "cat /hello");29 }30}31@ExtendWith(TestcontainersExtension.class)32public class SharedContainerTest {33 public static GenericContainer sharedContainer = new GenericContainer("alpine:3.9.3")34 .withCommand("tail", "-f", "/dev/null")35 .withSharedMemorySize(2 * 1024 *

Full Screen

Full Screen

findSharedContainers

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.junit.jupiter.api.extension.ExtendWith;3import org.testcontainers.containers.GenericContainer;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6import org.testcontainers.junit.jupiter.TestcontainersExtension;7import java.util.List;8import static org.hamcrest.CoreMatchers.is;9import static org.hamcrest.MatcherAssert.assertThat;10@ExtendWith(TestcontainersExtension.class)11public class TestContainersTest {12 private GenericContainer redis = new GenericContainer("redis:3.2.8").withExposedPorts(6379);13 public void test1() {14 System.out.println("test1");15 List<GenericContainer> sharedContainers = TestcontainersExtension.findSharedContainers(TestContainersTest.class);16 assertThat(sharedContainers.size(), is(1));17 assertThat(sharedContainers.get(0), is(redis));18 }19 public void test2() {20 System.out.println("test2");21 List<GenericContainer> sharedContainers = TestcontainersExtension.findSharedContainers(TestContainersTest.class);22 assertThat(sharedContainers.size(), is(1));23 assertThat(sharedContainers.get(0), is(redis));24 }25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful