English 中文(简体)
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.

最佳回答
问题回答

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




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签