使用 DataProvider 参数化的 JGiven 失败 "method 'name' should have no parameters"

JGiven parameterized with DataProvider fails with "method 'name' should have no parameters"

我正在为我的测试开发 JGiven 框架(已经是我的忠实粉丝了!)。我在使用 @DataProvider

时遇到问题

我正在浏览文档 http://jgiven.org/docs/parameterizedscenarios/ 但我收到以下错误

java.lang.Exception: Method test_update_order should have no parameters
        at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:76)
        at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:155)
        at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRunner.java:
208)
        at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.j
ava:188)
        at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunn
er.java:128)
        at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
        at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
        at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassR
unner.java:111)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorIm
pl.java:45)

我的测试class如下

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@Story( { "JIRA-123" } )
@Description("Story: coffee shop")
public class CoffeeShopTest extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {

    @Test @Issue( { "OUI-1156"} )
    @Description("Test for update order for coffee shop")
    @DataProvider( {
        "1",
        "3",
        "2",
    } )
    public void test_update_order(int coffee) {
        given()
            ...
        when()
            ...
        then()
            ...;
    }
}

谢谢!

为了使用 DataProvider,您必须用 @RunWith( DataProviderRunner.class) 注释测试 class。这在 JGiven 示例中不可见(我将相应地调整文档以使其更清楚)。有关如何使用数据提供程序的更多详细信息,请参阅 https://github.com/TNG/junit-dataprovider/wiki/Getting-started#usage

更新:我刚刚看到您已经在使用 spring junit runner。这意味着您不能使用 dataprovider runner,因为您不能在 junit 中使用两个 runner 进行相同的测试 class。虽然可以在没有 spring junit runner 的情况下执行 spring 测试,但这是一个不同的主题 ;-)

Update2:dataprovider runner的wiki描述了如何一起使用spring和dataprovider runner:https://github.com/TNG/junit-dataprovider/wiki/Tips-and-Tricks#dataprovider-for-spring-integration-testing