All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.spring.JqwikSpringExtension Maven / Gradle / Ivy

There is a newer version: 0.12.0
Show newest version
package net.jqwik.spring;

import java.lang.reflect.*;
import java.util.*;

import net.jqwik.api.*;
import net.jqwik.api.lifecycle.*;
import org.apiguardian.api.*;
import org.springframework.test.context.*;

/**
 * This class includes all the jqwik hooks necessary to use spring in examples and properties
 */
@API(status = API.Status.INTERNAL)
class JqwikSpringExtension implements RegistrarHook {

	public static TestContextManager getTestContextManager(Class containerClass) {
		return testContextManagerStore(containerClass).get();
	}

	private static Store testContextManagerStore(Class containerClass) {
		return getOrCreateTestContextManagerStore(containerClass);
	}

	@Override
	public boolean appliesTo(Optional optionalElement) {
		// Only apply to container classes
		return optionalElement.map(element -> element instanceof Class).orElse(false);
	}

	private static Store getOrCreateTestContextManagerStore(Class containerClass) {
		return Store.getOrCreate(
				storeIdentifier(containerClass),
				Lifespan.RUN,
				() -> new TestContextManager(containerClass)
		);
	}

	private static Tuple.Tuple2 storeIdentifier(Class containerClass) {
		return Tuple.of(JqwikSpringExtension.class, containerClass);
	}

	@Override
	public void registerHooks(Registrar registrar) {
		registrar.register(AroundSpringTestContainer.class, PropagationMode.NO_DESCENDANTS);
		registrar.register(OutsideLifecycleMethodsHook.class, PropagationMode.DIRECT_DESCENDANTS);
		registrar.register(InsideLifecycleMethodsHook.class, PropagationMode.DIRECT_DESCENDANTS);
		registrar.register(ResolveSpringParametersHook.class, PropagationMode.DIRECT_DESCENDANTS);
		registrar.register(EnabledIfHook.class, PropagationMode.DIRECT_DESCENDANTS);
		registrar.register(DisabledIfHook.class, PropagationMode.DIRECT_DESCENDANTS);
	}

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy