如何使用cucumber-jvm和guice进行多态测试?

时间:2017-01-13 02:34:13

标签: java dependency-injection inversion-of-control guice cucumber-jvm

我在JSF2中使用了一个简单的Web UI,以演示如何将黄瓜用作我的团队的BDD框架。该应用程序非常简单 - 您输入一个总和,它给你答案。现在我们只实现正整数 - 所以它实际上只是在总和框中回应了什么。

我正在使用页面模式,因此我定义了一组功能,这些功能达到了一组调用Home接口的步骤。我希望能够重用这些功能来测试不同级别的应用程序,方法是根据正在运行的测试注入Home接口的不同实现。例如,一个实现可能会启动selenium并使用WebDriver来驱动UI,另一个实现可能会直接调用java对象。

我有两个测试类:

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue=<somepackage>)
public class SeleniumTest {

}

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue=<somepackage>)
public class JavaTest {

}

我尝试过使用这种配置方法的模块:

protected void configure() {    
    install(new PrivateModule() {
        @Override
        protected void configure() {
            bind(Home.class).to(SeleniumHome.class);
            expose(SeleniumTest.class);
        }
    });
    install(new PrivateModule() {
        @Override
        protected void configure() {
            bind(Home.class).to(JavaHome.class);
            expose(JavaTest.class);
        }
    });
}

但是这给了我

initializationError(package.SeleniumTest): Failed to instantiate public cucumber.runtime.java.JavaBackend(cucumber.runtime.io.ResourceLoader) with [cucumber.runtime.io.MultiLoader@790da477]
initializationError(package.HomeTest): Failed to instantiate public cucumber.runtime.java.JavaBackend(cucumber.runtime.io.ResourceLoader) with [cucumber.runtime.io.MultiLoader@790da477]

如果我将模块更改为直接绑定,没有PrivateModule,那么(正如您所期望的那样)两个测试执行完全相同的步骤两次 - 但它确实绑定并且确实执行:

protected void configure() {    
    bind(Home.class).to(SeleniumHome.class);
}

同样有效:

protected void configure() {    
    bind(Home.class).to(JavaHome.class);
}

为完整起见,我的功能文件如下:

Feature: Literals story

Narrative:
In order to start to build up more complex expressions
As a parser user
I want to be able to use numbers directly in my expression string

Scenario:  This is a selection of positive literals we can test with
    Given I visit the webpage
    When I submit the expression 5
    Then the result should be 5

    When I submit the expression 6
    Then the result should be 6

我的步骤文件如下:

public class ParserSteps {

    private Home home;

    @Inject
    public ParserStepsImpl(Home home) {
        this.home = home;
    }

    @Given("I visit the webpage")
    public void theUserVisitsTheWebpage() {
        home.open();
    }

    @When("I submit the expression (.*)")
    public void theySubmitTheExpression(String value) {
        home.calculate(value);
    }

    @Then("the result should be (-?\\d+)")
    public void theResultShouldBe(Double value) {
        assertEquals(true, home.checkResult(value));
    }

}

0 个答案:

没有答案