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

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

Source:TestcontainersExtension.java Github

copy

Full Screen

...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(),160 isRestartContainer(),161 ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)162 .stream()163 .map(f -> getContainerInstance(testInstance, f));164 }165 private Predicate<Field> isRestartContainer() {166 return isContainer().and(ReflectionUtils::isNotStatic);167 }168 private static Predicate<Field> isContainer() {169 return field -> {170 boolean isAnnotatedWithContainer = AnnotationSupport.isAnnotated(field, Container.class);171 if (isAnnotatedWithContainer) {...

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1@ExtendWith(TestcontainersExtension.class)2class TestcontainersExtensionTest {3 void test() throws Exception {4 Method findRestartContainers = TestcontainersExtension.class.getDeclaredMethod("findRestartContainers", ExtensionContext.class);5 findRestartContainers.setAccessible(true);6 ExtensionContext context = mock(ExtensionContext.class);7 List<Container<?>> restartContainers = (List<Container<?>>) findRestartContainers.invoke(null, context);8 assertThat(restartContainers).isEmpty();9 }10}11@ExtendWith(TestcontainersExtension.class)12class TestcontainersExtensionTest {13 void test() throws Exception {14 Method findRestartContainers = TestcontainersExtension.class.getDeclaredMethod("findRestartContainers", ExtensionContext.class);15 findRestartContainers.setAccessible(true);16 ExtensionContext context = mock(ExtensionContext.class);17 when(context.getTestInstance()).thenReturn(this);18 List<Container<?>> restartContainers = (List<Container<?>>) findRestartContainers.invoke(null, context);19 assertThat(restartContainers).containsExactlyInAnyOrder(container1, container2);20 }21}22@ExtendWith(TestcontainersExtension.class)23class TestcontainersExtensionTest {

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1org.testcontainers.junit.jupiter.TestcontainersExtension.findRestartContainers() Method2public static Collection<Container<?>> findRestartContainers() {3 Collection<Container<?>> restartContainers = new ArrayList<>();4 for (Container<?> container : findContainers()) {5 if (container.shouldBeReused()) {6 restartContainers.add(container);7 }8 }9 return restartContainers;10}11org.testcontainers.junit.jupiter.TestcontainersExtension.findContainers() Method12org.testcontainers.junit.jupiter.TestcontainersExtension.findContainers(Class<T> containerClass) Method13org.testcontainers.junit.jupiter.TestcontainersExtension.getContainer(Class<T> containerClass) Method14org.testcontainers.junit.jupiter.TestcontainersExtension.getContainer(Class<T> containerClass, String containerAlias) Method15org.testcontainers.junit.jupiter.TestcontainersExtension.getContainer(String containerAlias) Method16org.testcontainers.junit.jupiter.TestcontainersExtension.getContainers() Method17org.testcontainers.junit.jupiter.TestcontainersExtension.getContainers(Class<T> containerClass) Method18org.testcontainers.junit.jupiter.TestcontainersExtension.getOrCreateContainer(Class<T> containerClass) Method19org.testcontainers.junit.jupiter.TestcontainersExtension.getOrCreateContainer(Class<T> containerClass, String containerAlias) Method20org.testcontainers.junit.jupiter.TestcontainersExtension.getOrCreateContainer(String containerAlias) Method21org.testcontainers.junit.jupiter.TestcontainersExtension.getOrCreateContainers() Method22org.testcontainers.junit.jupiter.TestcontainersExtension.getOrCreateContainers(Class<T> containerClass) Method23org.testcontainers.junit.jupiter.TestcontainersExtension.getTestDescription() Method24org.testcontainers.junit.jupiter.TestcontainersExtension.getTestDescription(ExtensionContext context) Method25org.testcontainers.junit.jupiter.TestcontainersExtension.getTestInstance() Method26org.testcontainers.junit.jupiter.TestcontainersExtension.getTestInstance(ExtensionContext context) Method27org.testcontainers.junit.jupiter.TestcontainersExtension.getTestStore() Method28org.testcontainers.junit.jupiter.TestcontainersExtension.getTestStore(ExtensionContext

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1package com.baeldung.testcontainers;2import org.junit.jupiter.api.Test;3import org.junit.jupiter.api.extension.ExtendWith;4import org.testcontainers.containers.GenericContainer;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.junit.jupiter.Testcontainers;7@ExtendWith(TestcontainersExtension.class)8public class TestcontainersExtensionUnitTest {9 private GenericContainer redis = new GenericContainer("redis:3.2.1").withExposedPorts(6379);10 public void whenRestartingContainer_thenItShouldBeRunning() {11 redis.stop();12 redis.start();13 assert redis.isRunning();14 }15}

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1public class TestcontainersExtensionTest {2 void findRestartContainers() {3 TestcontainersExtension extension = new TestcontainersExtension();4 List<Container> containers = extension.findRestartContainers();5 assertThat(containers).hasSize(1);6 assertThat(containers.get(0)).isInstanceOf(DockerComposeContainer.class);7 }8}9public class TestcontainersExtension implements BeforeAllCallback, AfterEachCallback, ParameterResolver {10 List<Container> findRestartContainers() {11 return containers.stream()12 .filter(container -> container.shouldBeReused())13 .collect(Collectors.toList());14 }15}16public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> extends GenericContainer<SELF> {17 public boolean shouldBeReused() {18 return true;19 }20}21public class GenericContainer<SELF extends GenericContainer<SELF>> extends Container<SELF> {22 public boolean shouldBeReused() {23 return false;24 }25}26public abstract class Container<SELF extends Container<SELF>> {27 public abstract boolean shouldBeReused();28}29public class TestcontainersExtensionTest {30 void findRestartContainers() {31 TestcontainersExtension extension = new TestcontainersExtension();32 List<Container> containers = extension.findRestartContainers();33 assertThat(containers).hasSize(1);34 assertThat(containers.get(0)).isInstanceOf(DockerComposeContainer.class);35 }36}37public class TestcontainersExtension implements BeforeAllCallback, AfterEachCallback, ParameterResolver {38 List<Container> findRestartContainers() {39 return containers.stream()40 .filter(container -> container.shouldBeReused())41 .collect(Collectors.toList());42 }43}44public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> extends GenericContainer<SELF> {45 public boolean shouldBeReused() {46 return true;47 }48}49public class GenericContainer<SELF extends GenericContainer<SELF>> extends Container<SELF> {50 public boolean shouldBeReused() {51 return false;52 }53}

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1 void shouldFindRestartContainers() {2 var testcontainersExtension = new TestcontainersExtension();3 var restartContainers = testcontainersExtension.findRestartContainers(ExampleContainer.class);4 assertThat(restartContainers).isNotEmpty();5 assertThat(restartContainers).containsExactly(ExampleContainer.class);6 }7 void shouldFindRestartContainersWhenAnnotationIsOnSuperClass() {8 var testcontainersExtension = new TestcontainersExtension();9 var restartContainers = testcontainersExtension.findRestartContainers(ExampleContainerSubclass.class);10 assertThat(restartContainers).isNotEmpty();11 assertThat(restartContainers).containsExactly(ExampleContainer.class);12 }13 void shouldFindRestartContainersWhenAnnotationIsOnSuperInterface() {14 var testcontainersExtension = new TestcontainersExtension();15 var restartContainers = testcontainersExtension.findRestartContainers(ExampleContainerSubInterface.class);16 assertThat(restartContainers).isNotEmpty();17 assertThat(restartContainers).containsExactly(ExampleContainer.class);18 }19 void shouldFindRestartContainersWhenAnnotationIsOnSuperSuperInterface() {20 var testcontainersExtension = new TestcontainersExtension();21 var restartContainers = testcontainersExtension.findRestartContainers(ExampleContainerSubSubInterface.class);22 assertThat(restartContainers).isNotEmpty();23 assertThat(restartContainers).containsExactly(ExampleContainer.class);24 }25 void shouldNotFindRestartContainers() {26 var testcontainersExtension = new TestcontainersExtension();27 var restartContainers = testcontainersExtension.findRestartContainers(AnotherExampleContainer.class);28 assertThat(restartContainers).isEmpty();29 }30 void shouldFindRestartContainersWhenAnnotationIsOnSuperClassAndSuperInterface() {31 var testcontainersExtension = new TestcontainersExtension();32 var restartContainers = testcontainersExtension.findRestartContainers(ExampleContainerSubclassSubInterface.class);33 assertThat(restartContainers).isNotEmpty();34 assertThat(restartContainers).containsExactly(ExampleContainer.class);35 }36 void shouldFindRestartContainersWhenAnnotationIsOnSuperClassAndSuperSuperInterface()

Full Screen

Full Screen

findRestartContainers

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.extension.AfterAllCallback2import org.junit.jupiter.api.extension.AfterEachCallback3import org.junit.jupiter.api.extension.BeforeAllCallback4import org.junit.jupiter.api.extension.BeforeEachCallback5import org.junit.jupiter.api.extension.ExtensionContext6import org.junit.jupiter.api.extension.ExtensionContext.Namespace7import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource8import org.junit.platform.commons.support.AnnotationSupport9import org.junit.platform.commons.support.ReflectionSupport10import org.junit.platform.commons.support.ReflectionSupport.HierarchyTraversalMode11import org.testcontainers.DockerClientFactory12import org.testcontainers.containers.Container13import org.testcontainers.containers.ContainerLaunchException14import org.testcontainers.containers.ContainerState15import org.testcontainers.containers.GenericContainer16import org.testcontainers.containers.Network17import org.testcontainers.containers.output.OutputFrame18import org.testcontainers.containers.output.OutputFrame.OutputType19import org.testcontainers.containers.output.ToStringConsumer20import org.testcontainers.containers.wait.strategy.WaitStrategy21import org.testcontainers.lifecycle.Startable22import org.testcontainers.lifecycle.TestDescription23import org.testcontainers.lifecycle.TestLifecycleAware24import org.testcontainers.lifecycle.TestLifecycleAwareContainer25import org.testcontainers.lifecycle.TestLifecycleAwareContainer.TestLifecycleAwareContainerStartable26import org.testcontainers.lifecycle.TestLifecycleAwareDockerClientAware27import org.testcontainers.lifecycle.TestLifecycleAwareNetwork28import org.testcontainers.lifecycle.TestLifecycleAwareNetwork.TestLifecycleAwareNetworkStartable29import org.testcontainers.lifecycle.TestLifecycleAwareStartable30import org.testcontainers.lifecycle.TestLifecycleAwareWaitStrategy31import org.testcontainers.lifecycle.TestLifecycleAwareWaitStrategy.TestLifecycleAwareWaitStrategyStartable32import org.testcontainers.utility.DockerImageName33import org.testcontainers.utility.LazyFuture34import org.testcontainers.utility.TestcontainersConfiguration35import org.testcontainers.utility.TestcontainersConfiguration.getInstance36import org.testcontainers.utility.TestcontainersConfiguration.getInstance().getDockerClientStrategyClassName37import org.testcontainers.utility.TestcontainersConfiguration.getInstance().getDockerClientStrategyClassName().newInstance38import org.testcontainers.utility.TestcontainersConfiguration.getInstance().getDockerClientStrategyClassName().newInstance().getDockerClient39import org.testcontainers.utility.TestcontainersConfiguration.getInstance().getDockerClientStrategyClassName().newInstance().getDockerClient().listContainersCmd40import org.testcontainers.utility.TestcontainersConfiguration.getInstance().getDockerClientStrategyClassName().newInstance().get

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