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

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

Source:TestcontainersExtension.java Github

copy

Full Screen

...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(...

Full Screen

Full Screen

collectParentTestInstances

Using AI Code Generation

copy

Full Screen

1class TestcontainersExtensionTest {2 private static final GenericContainer<?> container = new GenericContainer<>("alpine:3.7")3 .withCommand("sleep", "3600");4 void test() {5 TestcontainersExtension extension = new TestcontainersExtension();6 Object parentTestInstance = extension.collectParentTestInstances(7 new TestDescriptorStub("test", TestDescriptorStub.Type.TEST));8 System.out.println(parentTestInstance);9 }10}

Full Screen

Full Screen

collectParentTestInstances

Using AI Code Generation

copy

Full Screen

1public class ParentTest {2 private static final GenericContainer<?> container = new GenericContainer<>("alpine:3.8").withCommand("top");3 void parentTest() {4 System.out.println("parentTest");5 }6}7public class ChildTest extends ParentTest {8 void childTest() {9 System.out.println("childTest");10 }11}12public class ParentTest {13 private static final GenericContainer<?> container = new GenericContainer<>("alpine:3.8").withCommand("top");14 void parentTest() {15 System.out.println("parentTest");16 }17}18public class ChildTest extends ParentTest {19 void childTest() {20 System.out.println("childTest");21 }22}23public class TestcontainersExtensionTest {24 void collectParentTestInstances() {25 List<ParentTest> parentTests = TestcontainersExtension.collectParentTestInstances(ParentTest.class);26 parentTests.forEach(parentTest -> {27 try {28 parentTest.container.start();29 } catch (Exception e) {30 throw new RuntimeException(e);31 }32 });33 }34}

Full Screen

Full Screen

collectParentTestInstances

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.containers.Network;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.junit.jupiter.Testcontainers;7import java.util.List;8import java.util.stream.Collectors;9public class ParentTestInstanceTest {10 public static final GenericContainer<?> parentContainer = new GenericContainer<>()11 .withNetwork(Network.SHARED)12 .withNetworkAliases("parent")13 .withCommand("tail -f /dev/null");14 @ExtendWith(TestcontainersExtension.class)15 void test() {16 List<ParentTestInstanceTest> parentTestInstances = TestcontainersExtension.collectParentTestInstances(ParentTestInstanceTest.class);17 List<GenericContainer<?>> containers = parentTestInstances.stream()18 .map(testInstance -> testInstance.parentContainer)19 .collect(Collectors.toList());20 containers.forEach(GenericContainer::start);21 containers.forEach(GenericContainer::stop);22 }23}

Full Screen

Full Screen

collectParentTestInstances

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.PostgreSQLContainer;3import org.testcontainers.containers.MySQLContainer;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6import org.testcontainers.utility.DockerImageName;7import java.sql.Connection;8import java.sql.DriverManager;9import java.sql.SQLException;10import java.sql.Statement;11import java.util.List;12import java.util.stream.Collectors;13public class TestcontainersExample {14 private static final DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres");15 private static final DockerImageName MYSQL_IMAGE = DockerImageName.parse("mysql");

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