Class AbstractTestNGSpringContextTests
- java.lang.Object
- org.springframework.test.context.testng.AbstractTestNGSpringContextTests
- All Implemented Interfaces:
Aware
,ApplicationContextAware
,org.testng.IHookable
,org.testng.ITestNGListener
- Direct Known Subclasses:
AbstractTransactionalTestNGSpringContextTests
@TestExecutionListeners({ServletTestExecutionListener.class,DirtiesContextBeforeModesTestExecutionListener.class,DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class}) public abstract class AbstractTestNGSpringContextTests extends Object implements org.testng.IHookable, ApplicationContextAware
Abstract base test class which integrates the Spring TestContext Framework with explicitApplicationContext
testing support in a TestNG environment.Concrete subclasses:
- Typically declare a class-level
@ContextConfiguration
annotation to configure theapplication context
resource locations
orannotated classes
. If your test does not need to load an application context, you may choose to omit the@ContextConfiguration
declaration and to configure the appropriateTestExecutionListeners
manually. - Must have constructors which either implicitly or explicitly delegate to
super();
.
The following
TestExecutionListeners
are configured by default:- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
ContextConfiguration
,TestContext
,TestContextManager
,TestExecutionListeners
,ServletTestExecutionListener
,DirtiesContextBeforeModesTestExecutionListener
,DependencyInjectionTestExecutionListener
,DirtiesContextTestExecutionListener
,AbstractTransactionalTestNGSpringContextTests
,AbstractJUnit4SpringContextTests
Field Summary
Fields Modifier and Type Field Description protected ApplicationContext
applicationContext
TheApplicationContext
that was injected into this test instance viasetApplicationContext(ApplicationContext)
.protected Log
logger
Logger available to subclasses
Constructor Summary
Constructors Constructor Description AbstractTestNGSpringContextTests()
Construct a new AbstractTestNGSpringContextTests instance and initialize the internalTestContextManager
for the current test.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
run(org.testng.IHookCallBack callBack, org.testng.ITestResult testResult)
Delegates to thetest method
in the suppliedcallback
to execute the actual test and then tracks the exception thrown during test execution, if any.void
setApplicationContext(ApplicationContext applicationContext)
Set theApplicationContext
to be used by this test instance, provided viaApplicationContextAware
semantics.protected void
springTestContextAfterTestClass()
Delegates to the configuredTestContextManager
to call'after test class'
callbacks.protected void
springTestContextAfterTestMethod(Method testMethod)
Delegates to the configuredTestContextManager
topost-process
the test method after the actual test has executed.protected void
springTestContextBeforeTestClass()
Delegates to the configuredTestContextManager
to call'before test class'
callbacks.protected void
springTestContextBeforeTestMethod(Method testMethod)
Delegates to the configuredTestContextManager
topre-process
the test method before the actual test is executed.protected void
springTestContextPrepareTestInstance()
Delegates to the configuredTestContextManager
toprepare
this test instance prior to execution of any individual tests, for example for injecting dependencies, etc.
Field Detail
applicationContext
protected ApplicationContext applicationContext
TheApplicationContext
that was injected into this test instance viasetApplicationContext(ApplicationContext)
.
Constructor Detail
AbstractTestNGSpringContextTests
public AbstractTestNGSpringContextTests()
Construct a new AbstractTestNGSpringContextTests instance and initialize the internalTestContextManager
for the current test.
Method Detail
setApplicationContext
public final void setApplicationContext(ApplicationContext applicationContext)
Set theApplicationContext
to be used by this test instance, provided viaApplicationContextAware
semantics.- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Parameters:
applicationContext
- the ApplicationContext that this test runs in- See Also:
BeanInitializationException
springTestContextBeforeTestClass
protected void springTestContextBeforeTestClass() throws Exception
Delegates to the configuredTestContextManager
to call'before test class'
callbacks.- Throws:
Exception
- if a registered TestExecutionListener throws an exception
springTestContextPrepareTestInstance
protected void springTestContextPrepareTestInstance() throws Exception
Delegates to the configuredTestContextManager
toprepare
this test instance prior to execution of any individual tests, for example for injecting dependencies, etc.- Throws:
Exception
- if a registered TestExecutionListener throws an exception
springTestContextBeforeTestMethod
protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception
Delegates to the configuredTestContextManager
topre-process
the test method before the actual test is executed.- Parameters:
testMethod
- the test method which is about to be executed.- Throws:
Exception
- allows all exceptions to propagate.
run
public void run(org.testng.IHookCallBack callBack, org.testng.ITestResult testResult)
Delegates to thetest method
in the suppliedcallback
to execute the actual test and then tracks the exception thrown during test execution, if any.- Specified by:
run
in interfaceorg.testng.IHookable
- See Also:
IHookable.run(org.testng.IHookCallBack, org.testng.ITestResult)
springTestContextAfterTestMethod
protected void springTestContextAfterTestMethod(Method testMethod) throws Exception
Delegates to the configuredTestContextManager
topost-process
the test method after the actual test has executed.- Parameters:
testMethod
- the test method which has just been executed on the test instance- Throws:
Exception
- allows all exceptions to propagate
springTestContextAfterTestClass
protected void springTestContextAfterTestClass() throws Exception
Delegates to the configuredTestContextManager
to call'after test class'
callbacks.- Throws:
Exception
- if a registered TestExecutionListener throws an exception