Spring Dependency Injection with TestNG

Spring support JUnit quite well on that: With the RunWith and ContextConfiguration annotation, things look very intuitive

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")

This test will be able to run both in Eclipse & Maven in correctly. I wonder if there is similar stuff for TestNG. I'm considering moving to this "Next Generation" Framework but I didn't find a match for testing with Spring.


It works with TestNG as well. Your test class needs to extend one of the following classes:

  • org.springframework.test.context.testng.AbstractTestNGSpringContextTests
  • org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests

Here is an example that worked for me:

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 {

    public void testNullParamValidation() {
        // Testing code goes here!
    }
}

Spring and TestNG work well together, but there are some things to be aware of. Aside from subclassing AbstractTestNGSpringContextTests, you need to be aware of how it interacts with standard TestNG setup/teardown annotations.

TestNG has four levels of setup

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod

which occur exactly as you would expect (great example of self-documenting APIs). These all have an optional value called dependsOnMethods which can take a String or String[], which is the name or name(s) of the methods at the same level.

The AbstractTestNGSpringContextTests class has a BeforeClass annotated method called springTestContextPrepareTestInstance, which you must set your setup method to depend on if you are using an autowired class in it. For methods, you don't have to worry about the autowiring, since it occurs when the test class is setup in that before class method.

This just leaves the question of how you might use an autowired class in a method annotated with BeforeSuite. You can do this by manually calling springTestContextPrepareTestInstance - while its not setup by default to do this, I've done it several times successfully.

So, to illustrate, a modified version of Arup's 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(){
        // Test class setup code with autowired classes goes here
    }

    @Test
    public void testNullParamValidation() {
        // Testing code goes here!
    }
}