Spring Dependency Injection with TestNG

Spring supporting JUnit is pretty good: With RunWith and ContextConfiguration everything looks very intuitive

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

This test will work correctly in both Eclipse and Maven. I wonder if there are similar materials for TestNG. I am considering moving to this “next generation” platform, but I have not found a match for testing with Spring.

+43
spring junit testng
Apr 09 2018-10-09T00:
source share
3 answers
+49
Apr 09 '10 at 15:14
source share

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! } } 
+23
Apr 02 '12 at 16:59
source share

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(){ // Test class setup code with autowired classes goes here } @Test public void testNullParamValidation() { // Testing code goes here! } } 
+17
May 10 '13 at 2:55
source share



All Articles