Spring Dependency Injection with TestNG

JunitSpringTestng

Junit Problem Overview


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.

Junit Solutions


Solution 1 - Junit

Solution 2 - Junit

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!
    }
}

Solution 3 - Junit

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!
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhương NguyễnView Question on Stackoverflow
Solution 1 - JunitlexicoreView Answer on Stackoverflow
Solution 2 - JunitArup MalakarView Answer on Stackoverflow
Solution 3 - JunitromearaView Answer on Stackoverflow