弹簧支持JUnit很好的在于:与 RunWithContextConfiguration 注,事情看起来非常直观的

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

这种试验将能够运行这两个在日食&家在正确的。我不知道是否有类似的东西TestNG.我考虑搬到这"下一代"框架,但是我没找到匹配的试验用弹簧。

有帮助吗?

其他提示

下面是为我工作的示例:

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

弹簧和TestNG一起工作很好,但也有一些东西是知道的。除了继承AbstractTestNGSpringContextTests,你需要意识到它是如何与标准TestNG安装/拆卸的注释。

TestNG有四个层次的安装

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod

而发生的正是你所期望的(很好的例子自记录Api)。这些都有一个可选择的价值被称为"dependsOnMethods"这可能需要一串或串[],其姓名或名称(s)的方法在同一水平。

该AbstractTestNGSpringContextTests类有一个BeforeClass附加说明的方法称为springTestContextPrepareTestInstance,你必须设置安装的方法取决于如果您使用的是自动连接类。为方式,你不必担心自动装配,因为它发生时,该测试类是建立在这之前类方法。

这只是树叶这个问题的你怎么可能使用一个自动连接类方法的附加说明与BeforeSuite.你可以通过手动呼吁springTestContextPrepareTestInstance-虽然它不是安装默认情况下要做到这一点,我已经做了几次成功。

因此,为了说明,一个修改版本的Arup的例子:

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!
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top