/** * Test for SPR-10277 (multiple method chaining when building MockMvc). * * @author Wesley Hall */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration public class MockMvcBuilderMethodChainTests { @Autowired private WebApplicationContext wac; @Test public void chainMultiple() { MockMvcBuilders .webAppContextSetup(wac) .addFilter(new CharacterEncodingFilter() ) .defaultRequest(get("/").contextPath("/mywebapp")) .build(); } @Configuration @EnableWebMvc static class WebConfig implements WebMvcConfigurer { } }
/** * Construct a new {@link ContextConfigurationAttributes} instance for the * supplied {@link ContextConfiguration @ContextConfiguration} annotation and * the {@linkplain Class test class} that declared it. * @param declaringClass the test class that declared {@code @ContextConfiguration} * @param contextConfiguration the annotation from which to retrieve the attributes */ public ContextConfigurationAttributes(Class<?> declaringClass, ContextConfiguration contextConfiguration) { this(declaringClass, contextConfiguration.locations(), contextConfiguration.classes(), contextConfiguration.inheritLocations(), contextConfiguration.initializers(), contextConfiguration.inheritInitializers(), contextConfiguration.name(), contextConfiguration.loader()); }
@Override protected void initializeProcessEngine() { ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class); String[] value = contextConfiguration.value(); boolean hasOneArg = value != null && value.length == 1; String key = hasOneArg ? value[0] : ProcessEngine.class.getName(); ProcessEngine engine = this.cachedProcessEngines.containsKey(key) ? this.cachedProcessEngines.get(key) : this.applicationContext.getBean(ProcessEngine.class); this.cachedProcessEngines.put(key, engine); this.processEngine = engine; }
@RunWith(SpringRunner.class) @ContextConfiguration static abstract class BaseTestCase { @Configuration static class Config { /* no beans */ } @Autowired protected ApplicationContext applicationContext; protected void assertApplicationContextWasAutowired() { assertNotNull("The application context should have been autowired.", this.applicationContext); } }
@Ignore("TestCase classes are run manually by the enclosing test class") @RunWith(SpringRunner.class) @ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml") @Transactional public static class FailingAfterTransactionTestCase { @Test public void testNothing() { } @AfterTransaction public void afterTransaction() { fail("always failing afterTransaction()"); } }
@ContextConfiguration(locations = "classpath:ctx-repo-common-test-main.xml") @DirtiesContext @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestCommandLine extends AbstractIntegrationTest { @Autowired private CommandLineScriptExecutor commandLineScriptExecutor;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext public class PrimaryDataSourceTests {
@ContextConfiguration @WebAppConfiguration public class ServletTestExecutionListenerTestNGIntegrationTests extends AbstractTestNGSpringContextTests { @Autowired private MockHttpServletRequest servletRequest;
@ContextConfiguration(classes = NestedConfig.class) public class NestedTestCase extends SpringRuleConfigurer { @Autowired String bar; @Test public void nestedTest() throws Exception { // Note: the following would fail since TestExecutionListeners in // the Spring TestContext Framework are not applied to the enclosing // instance of an inner test class. // // assertEquals("foo", foo); assertNull("@Autowired field in enclosing instance should be null.", foo); assertEquals("bar", bar); } }
/** * Integration tests used to assess claims raised in * <a href="https://jira.spring.io/browse/SPR-9799" target="_blank">SPR-9799</a>. * * @author Sam Brannen * @since 3.2 * @see Spr9799AnnotationConfigTests */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class Spr9799XmlConfigTests { @Test public void applicationContextLoads() { // nothing to assert: we just want to make sure that the context loads without // errors. } }
/** * Integration tests used to assess claims raised in * <a href="https://jira.spring.io/browse/SPR-9799" target="_blank">SPR-9799</a>. * * @author Sam Brannen * @since 3.2 * @see Spr9799XmlConfigTests */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // NOTE: if we omit the @WebAppConfiguration declaration, the ApplicationContext will fail // to load since @EnableWebMvc requires that the context be a WebApplicationContext. @WebAppConfiguration public class Spr9799AnnotationConfigTests { @Configuration @EnableWebMvc static class Config { /* intentionally no beans defined */ } @Test public void applicationContextLoads() { // no-op } }
/** * @author Radovan Semancik */ @ContextConfiguration(locations = {"classpath:ctx-model-intest-test-main.xml"}) @DirtiesContext(classMode = ClassMode.AFTER_CLASS) @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestManualCapabilities extends TestManual { @Override protected File getResourceFile() { return RESOURCE_MANUAL_CAPABILITIES_FILE; } @Override protected boolean nativeCapabilitiesEntered() { return true; } @Override protected int getNumberOfAccountAttributeDefinitions() { return 5; } }
/** * Integration tests that verify support for custom SQL script syntax * configured via {@link SqlConfig @SqlConfig}. * * @author Sam Brannen * @since 4.1 */ @ContextConfiguration(classes = EmptyDatabaseConfig.class) @DirtiesContext public class CustomScriptSyntaxSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests { @Test @Sql("schema.sql") @Sql(scripts = "data-add-users-with-custom-script-syntax.sql",// config = @SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")) public void methodLevelScripts() { assertNumUsers(3); } protected void assertNumUsers(int expected) { assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); } }
@ContextHierarchy({// // @ContextConfiguration(locations = "3-A.xml", name = "alpha"),// @ContextConfiguration(locations = "3-B.xml", name = "beta"),// @ContextConfiguration(locations = "3-C.xml", name = "gamma") // }) private static class TestClass3WithMultiLevelContextHierarchy extends TestClass2WithMultiLevelContextHierarchy { }
/** * @author Sam Brannen * @since 4.3 */ @WebAppConfiguration @ContextConfiguration(classes = Config.class) interface WebAppConfigurationTestInterface { @Configuration static class Config { /* no user beans required for these tests */ } }
@Test public void contextConfigurationLocationsAndInheritLocations() throws Exception { Class<MetaLocationsConfigTestCase> declaringClass = MetaLocationsConfigTestCase.class; AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass, ContextConfiguration.class); assertNotNull(descriptor); assertEquals(declaringClass, descriptor.getRootDeclaringClass()); assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType()); assertEquals(ContextConfiguration.class, descriptor.getAnnotationType()); assertNotNull(descriptor.getComposedAnnotation()); assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType()); // direct access to annotation attributes: assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations()); assertFalse(descriptor.getAnnotation().inheritLocations()); }
protected String getSpringContextLocation() { if (this.getClass().isAnnotationPresent(ContextConfiguration.class)) { ContextConfiguration config = this.getClass().getAnnotation(ContextConfiguration.class); String[] locations = config.locations(); return StringUtil.unsplit(",", locations); } else { return DEFAULT_SPRING_CONTEXT_LOCATION; } }
/** * Integration test class that verifies proper detection of a default * XML config file even though a suitable Groovy script exists. * * @author Sam Brannen * @since 4.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class DefaultScriptDetectionXmlSupersedesGroovySpringContextTests { @Autowired protected String foo; @Test public final void foo() { assertEquals("The foo field should have been autowired.", "Foo", this.foo); } }
@Ignore("TestCase classes are run manually by the enclosing test class") @RunWith(SpringRunner.class) @ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml") @Transactional public static class FailingBeforeTransactionTestCase { @Test public void testNothing() { } @BeforeTransaction public void beforeTransaction() { fail("always failing beforeTransaction()"); } }
@ContextConfiguration(locations = "classpath:ctx-security-test-main.xml") @DirtiesContext @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestEmpty extends AbstractIntegrationTest { @Autowired(required=true) private MidPointPrincipalManagerMock userProfileService; @Autowired(required = true) private Clock clock;