Spring and TestNG work well together, but there are some things to be aware of. Besides the subclass of AbstractTestNGSpringContextTests, you need to know how it interacts with the standard TESTNG install / uninstall annotations.
TestNG has four levels of customization
- Beforesuite
- Beforetest
- Beforeclass
- Beforemethod
which happen exactly as you would expect (great example of self-documenting APIs). They all have an optional value called "dependOnMethods", which can take a string or string [], which is the name or names of methods at the same level.
The AbstractTestNGSpringContextTests class has an annotated BeforeClass method called springTestContextPrepareTestInstance, which you must set so that your installation method depends on whether you use an auto-programming class in it. For methods, you do not need to worry about auto-pause, as this happens when the test class is configured in this class method.
This just leaves the question of how to use an auto-level class in a method annotated with BeforeSuite. You can do this by manually calling springTestContextPrepareTestInstance - until it is configured by default for this, I have done this several times successfully.
So, to illustrate, a modified version of the Arup example:
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.Test; @Test @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class TestValidation extends AbstractTestNGSpringContextTests { @Autowired private IAutowiredService autowiredService; @BeforeClass(dependsOnMethods={"springTestContextPrepareTestInstance"}) public void setupParamValidation(){
romeara May 10 '13 at 2:55 a.m.
source share