Spring Dependency Injection with TestNG

前端 未结 3 495
难免孤独
难免孤独 2020-12-02 10:11

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

@RunWith(         


        
相关标签:
3条回答
  • 2020-12-02 10:52

    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!
        }
    }
    
    0 讨论(0)
  • 2020-12-02 10:54

    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!
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:00

    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
    0 讨论(0)
提交回复
热议问题