Refine search
@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); } }
@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); } }
/** * 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. } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext public class PrimaryDataSourceTests {
/** * 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")); } }
/** * 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); } }
/** * JUnit 4 based integration test for verifying support for the * {@link ContextConfiguration#inheritLocations() inheritLocations} flag of * {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a * href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896" * target="_blank">SPR-3896</a>. * * @author Sam Brannen * @since 2.5 */ @ContextConfiguration("DefaultLocationsInheritedTests-context.xml") public class ExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests { @Autowired private Pet pet; @Test public void verifyPetSetFromExtendedContextConfig() { assertNotNull("The pet should have been autowired.", this.pet); assertEquals("Fido", this.pet.getName()); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class LookUpNonexistentTxMgrTests {
@RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional @Sql({ "schema.sql", "data.sql" }) @DirtiesContext public class TransactionalSqlScriptsTests {
/** * Transactional integration tests that verify rollback semantics for * {@link Sql @Sql} support. * * @author Sam Brannen * @since 4.1 */ @ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class) @DirtiesContext public class PopulatedSchemaTransactionalSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests { @BeforeTransaction @AfterTransaction public void verifyPreAndPostTransactionDatabaseState() { assertNumUsers(0); } @Test @SqlGroup(@Sql("data-add-dogbert.sql")) public void methodLevelScripts() { assertNumUsers(1); } protected void assertNumUsers(int expected) { assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); } }
/** * Integration tests that verify support for configuration classes in * the Spring TestContext Framework in conjunction with the * {@link DelegatingSmartContextLoader}. * * @author Sam Brannen * @since 3.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DefaultLoaderDefaultConfigClassesInheritedTests.Config.class) public class DefaultLoaderExplicitConfigClassesInheritedTests extends DefaultLoaderExplicitConfigClassesBaseTests { @Autowired private Pet pet; @Test public void verifyPetSetFromExtendedContextConfig() { assertNotNull("The pet should have been autowired.", this.pet); assertEquals("Fido", this.pet.getName()); } }
/** * JUnit 4 based integration test for verifying support for the * {@link ContextConfiguration#inheritLocations() inheritLocations} flag of * {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a * href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896" * target="_blank">SPR-3896</a>. * * @author Sam Brannen * @since 2.5 */ @ContextConfiguration public class DefaultLocationsInheritedTests extends DefaultLocationsBaseTests { @Autowired private Pet pet; @Test public void verifyPetSetFromExtendedContextConfig() { assertNotNull("The pet should have been autowired.", this.pet); assertEquals("Fido", this.pet.getName()); } }
/** * This name of this class intentionally does not end with "Test" or "Tests" * since it should only be run as part of the test suite: {@link Spr8849Tests}. * * @author Sam Brannen * @since 4.2 * @see Spr8849Tests */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TestClass4 { @Configuration @ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config-with-auto-generated-db-name.xml") static class Config { } @Resource DataSource dataSource; @Test public void dummyTest() { assertNotNull(dataSource); } }
@RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Sql({ "schema.sql", "data.sql" }) @DirtiesContext public class NonTransactionalSqlScriptsTests {
@ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class) @DirtiesContext public class IsolatedTransactionModeSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests {
/** * Integration tests that verify support for configuration classes in * the Spring TestContext Framework in conjunction with the * {@link DelegatingSmartContextLoader}. * * @author Sam Brannen * @since 3.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DefaultLoaderDefaultConfigClassesBaseTests.Config.class) public class DefaultLoaderExplicitConfigClassesBaseTests { @Autowired protected Employee employee; @Test public void verifyEmployeeSetFromBaseContextConfig() { assertNotNull("The employee should have been autowired.", this.employee); assertEquals("John Smith", this.employee.getName()); } }
/** * Subclass #1 of {@link BaseAppCtxRuleTests}. * * @author Sam Brannen * @since 4.2 */ @ContextConfiguration public class Subclass1AppCtxRuleTests extends BaseAppCtxRuleTests { @Autowired private String bar; @Test public void bar() { assertEquals("bar", bar); } @Configuration static class Config { @Bean public String bar() { return "bar"; } } }
/** * This name of this class intentionally does not end with "Test" or "Tests" * since it should only be run as part of the test suite: {@link Spr8849Tests}. * * @author Sam Brannen * @since 4.2 * @see Spr8849Tests */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TestClass3 { @Configuration @ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config-with-auto-generated-db-name.xml") static class Config { } @Resource DataSource dataSource; @Test public void dummyTest() { assertNotNull(dataSource); } }
@RunWith(SpringJUnit4ClassRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @ContextConfiguration(classes = EmptyDatabaseConfig.class) @Transactional @Sql( statements = "INSERT INTO user VALUES('Dilbert')" @DirtiesContext public class TransactionalInlinedStatementsSqlScriptsTests {
/** * @author Sam Brannen * @since 4.3 */ @ContextConfiguration(classes = EmptyDatabaseConfig.class) @DirtiesContext @SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@") interface SqlConfigTestInterface { }