/** * Create a new {@link TestContextManager} for the supplied test class. * <p>Can be overridden by subclasses. * @param clazz the test class to be managed */ protected TestContextManager createTestContextManager(Class<?> clazz) { return new TestContextManager(clazz); }
/** * Construct a new AbstractTestNGSpringContextTests instance and initialize * the internal {@link TestContextManager} for the current test class. */ public AbstractTestNGSpringContextTests() { this.testContextManager = new TestContextManager(getClass()); }
public SpringActivitiTestCase() { this.testContextManager = new TestContextManager(getClass()); }
@Test(expected = AnnotationConfigurationException.class) public void listenersAndValueAttributesDeclared() { new TestContextManager(DuplicateListenersConfigTestCase.class); }
private ExtensionContext buildExtensionContext(String methodName) { Class<?> testClass = SpringTestCase.class; Method method = ReflectionUtils.findMethod(getClass(), methodName); Store store = mock(Store.class); when(store.getOrComputeIfAbsent(any(), any(), any())).thenReturn(new TestContextManager(testClass)); ExtensionContext extensionContext = mock(ExtensionContext.class); when(extensionContext.getTestClass()).thenReturn(Optional.of(testClass)); when(extensionContext.getElement()).thenReturn(Optional.of(method)); when(extensionContext.getStore(any())).thenReturn(store); return extensionContext; }
public SpringTestContextManager(Class<?> testClass) { delegate = new TestContextManager(testClass); if (!testContextBootstrapperAvailable) { delegate.registerTestExecutionListeners(new SpringMockTestExecutionListener()); } }
private void assertNumRegisteredListeners(Class<?> testClass, int expected) { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("Num registered TELs for " + testClass, expected, testContextManager.getTestExecutionListeners().size()); }
private void assertRegisteredListeners(Class<?> testClass, List<Class<?>> expected) { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("TELs registered for " + testClass.getSimpleName(), names(expected), names(classes(testContextManager))); }
private void test(String useCase, Class<?> testClass, Callback callback) throws Exception { TestContextManager testContextManager = new TestContextManager(testClass); assertEquals("Registered TestExecutionListeners", 2, testContextManager.getTestExecutionListeners().size()); try { Method testMethod = getClass().getMethod("toString"); callback.invoke(testContextManager, testClass, testMethod); fail("should have thrown an AssertionError"); } catch (AssertionError err) { // 'after' callbacks are reversed, so 2 comes before 1. assertEquals(useCase + "-2", err.getMessage()); Throwable[] suppressed = err.getSuppressed(); assertEquals(1, suppressed.length); assertEquals(useCase + "-1", suppressed[0].getMessage()); } }
@Test public void invokeTestContextManagerFromConcurrentThreads() { TestContextManager tcm = new TestContextManager(TestCase.class); // Run the actual test several times in order to increase the chance of threads // stepping on each others' toes by overwriting the same mutable state in the // TestContext. IntStream.range(1, 20).forEach(i -> { actualMethods.clear(); // Execute TestExecutionListener in parallel, thereby simulating parallel // test method execution. stream(TestCase.class.getDeclaredMethods()).parallel().forEach(testMethod -> { try { tcm.beforeTestClass(); tcm.beforeTestMethod(testInstance, testMethod); // no need to invoke the actual test method tcm.afterTestMethod(testInstance, testMethod, null); tcm.afterTestClass(); } catch (Exception ex) { throw new RuntimeException(ex); } }); assertThat(actualMethods, equalTo(expectedMethods)); }); assertEquals(0, tcm.getTestContext().attributeNames().length); }
public SpringProcessEngineTestCase() { super(); this.testContextManager = new TestContextManager(getClass()); }
@RunWith(Parameterized.class) @ContextConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { private TestContextManager testContextManager; @Before public void setUpContext() throws Exception { //this is where the magic happens, we actually do "by hand" what the spring runner would do for us, // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this); } ... }
testContextManager = new TestContextManager(getClass()); testContextManager.prepareTestInstance(this);
/** * Create a new {@link TestContextManager} for the supplied test class. * <p>Can be overridden by subclasses. * @param clazz the test class to be managed */ protected TestContextManager createTestContextManager(Class<?> clazz) { return new TestContextManager(clazz); }
/** * Construct a new AbstractTestNGSpringContextTests instance and initialize * the internal {@link TestContextManager} for the current test class. */ public AbstractTestNGSpringContextTests() { this.testContextManager = new TestContextManager(getClass()); }
@RunWith(Parameterized.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public abstract class BaseRepositoryTest extends AbstractJUnit4SpringContextTests { private TestContextManager testContextManager; @Before public void setUpContext() throws Exception { this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this); } }
@RunWith(Parameterized.class) @ContextConfiguration(locations = "classpath:/spring-context.xml") public class MyTestClass { @Parameters public static Collection data() { // return parameters, following pattern in // http://junit.org/apidocs/org/junit/runners/Parameterized.html } @Before public void setUp() throws Exception { new TestContextManager(getClass()).prepareTestInstance(this); } }
public abstract class RunWithSpringJUnit4 { private TestContextManager testContextManager; public RunWithSpringJUnit4() { try { this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this); } catch (Exception e) { e.printStackTrace(); } } }
private SpringZuchini(Class<?> testClass, ScopeExecutionListener scopeExecutionListener) throws Exception { super(testClass, Collections.<Runner>singletonList(delegate(testClass, scopeExecutionListener))); this.testContextManager = new TestContextManager(testClass); this.testContextManager.registerTestExecutionListeners(scopeExecutionListener); }
protected TestContextManager createTestContextManager(Class<?> testClass) { TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.createBootstrapContext(testClass)); return new TestContextManager(testContextBootstrapper); }