spring-framework / org.springframework.test.context.junit4 / AbstractJUnit4SpringContextTests

AbstractJUnit4SpringContextTests

@TestExecutionListeners([ServletTestExecutionListener, DirtiesContextBeforeModesTestExecutionListener, DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener]) abstract class AbstractJUnit4SpringContextTests : ApplicationContextAware

Abstract base test class which integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a JUnit environment.

Concrete subclasses should typically declare a class-level ContextConfiguration annotation to configure the ApplicationContext or . If your test does not need to load an application context, you may choose to omit the ContextConfiguration declaration and to configure the appropriate org.springframework.test.context.TestExecutionListener manually.

The following org.springframework.test.context.TestExecutionListener are configured by default:

This class serves only as a convenience for extension.

NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.

Author
Sam Brannen

Since
2.5

See Also
ContextConfigurationTestContextTestContextManagerTestExecutionListenersServletTestExecutionListenerDirtiesContextBeforeModesTestExecutionListenerDependencyInjectionTestExecutionListenerDirtiesContextTestExecutionListenerAbstractTransactionalJUnit4SpringContextTestsorg.springframework.test.context.testng.AbstractTestNGSpringContextTests

Constructors

<init>

AbstractJUnit4SpringContextTests()

Abstract base test class which integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a JUnit environment.

Concrete subclasses should typically declare a class-level ContextConfiguration annotation to configure the ApplicationContext or . If your test does not need to load an application context, you may choose to omit the ContextConfiguration declaration and to configure the appropriate org.springframework.test.context.TestExecutionListener manually.

The following org.springframework.test.context.TestExecutionListener are configured by default:

  • org.springframework.test.context.web.ServletTestExecutionListener
  • org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
  • org.springframework.test.context.support.DependencyInjectionTestExecutionListener
  • org.springframework.test.context.support.DirtiesContextTestExecutionListener

This class serves only as a convenience for extension.

  • If you do not wish for your test classes to be tied to a Spring-specific class hierarchy, you may configure your own custom test classes by using SpringRunner, ContextConfiguration, TestExecutionListeners, etc.
  • If you wish to extend this class and use a runner other than the SpringRunner, as of Spring Framework 4.2 you can use org.springframework.test.context.junit4.rules.SpringClassRule and org.springframework.test.context.junit4.rules.SpringMethodRule and specify your runner of choice via RunWith.

NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.

Functions

setApplicationContext

fun setApplicationContext(applicationContext: ApplicationContext): Unit

Set the ApplicationContext to be used by this test instance, provided via ApplicationContextAware semantics.

Inheritors

AbstractTransactionalJUnit4SpringContextTests

abstract class AbstractTransactionalJUnit4SpringContextTests : AbstractJUnit4SpringContextTests

Abstract Transactional extension of AbstractJUnit4SpringContextTests which adds convenience functionality for JDBC access. Expects a DataSource bean and a PlatformTransactionManager bean to be defined in the Spring ApplicationContext.

This class exposes a JdbcTemplate and provides an easy way to count the number of rows in a table (potentially with a WHERE clause), delete from tables, drop tables, and execute SQL scripts within a transaction.

Concrete subclasses must fulfill the same requirements outlined in AbstractJUnit4SpringContextTests.

The following org.springframework.test.context.TestExecutionListener are configured by default:

  • org.springframework.test.context.web.ServletTestExecutionListener
  • org.springframework.test.context.support.DependencyInjectionTestExecutionListener
  • org.springframework.test.context.support.DirtiesContextTestExecutionListener
  • org.springframework.test.context.transaction.TransactionalTestExecutionListener
  • org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener

This class serves only as a convenience for extension.

  • If you do not wish for your test classes to be tied to a Spring-specific class hierarchy, you may configure your own custom test classes by using SpringRunner, ContextConfiguration, TestExecutionListeners, etc.
  • If you wish to extend this class and use a runner other than the SpringRunner, as of Spring Framework 4.2 you can use org.springframework.test.context.junit4.rules.SpringClassRule and org.springframework.test.context.junit4.rules.SpringMethodRule and specify your runner of choice via org.junit.runner.RunWith.

NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.