Java 如何让多态测试与jvm和guice一起工作?
为了向我的团队演示如何使用cucumber作为BDD框架,我在JSF2中提供了一个简单的web UI。这个应用程序非常简单——你输入一个总数,它就会给你答案。现在我们只实现正整数,所以它实际上只是在和框中回显什么 我使用的是pages模式,所以我定义了一组特性,这些特性涉及到一组步骤,这些步骤调用主界面。我希望能够重用这些特性,通过根据正在运行的测试注入不同的主界面实现来测试不同级别的应用程序。例如,一个实现可能启动selenium并使用WebDriver来驱动UI,另一个实现可能直接调用java对象 我有两个测试班:Java 如何让多态测试与jvm和guice一起工作?,java,dependency-injection,inversion-of-control,guice,cucumber-jvm,Java,Dependency Injection,Inversion Of Control,Guice,Cucumber Jvm,为了向我的团队演示如何使用cucumber作为BDD框架,我在JSF2中提供了一个简单的web UI。这个应用程序非常简单——你输入一个总数,它就会给你答案。现在我们只实现正整数,所以它实际上只是在和框中回显什么 我使用的是pages模式,所以我定义了一组特性,这些特性涉及到一组步骤,这些步骤调用主界面。我希望能够重用这些特性,通过根据正在运行的测试注入不同的主界面实现来测试不同级别的应用程序。例如,一个实现可能启动selenium并使用WebDriver来驱动UI,另一个实现可能直接调用jav
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue=<somepackage>)
public class SeleniumTest {
}
但这给了我
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));
}
}
我的steps文件如下所示:
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));
}
}
“取决于运行哪个测试”。不要用Guice测试。您应该测试真正的代码,而不是注入。如果需要,使用mock。你误解了-我不是在测试guice。我想做的是以两种方式有效地实现这些步骤,因此给定一个具有指定表达式的特性,当运行
JavaTest
时,注入JavaHome
并直接调用Parser.calculate(String expression)
方法。当运行SeleniumTest
时,将注入SeleniumHome
,启动浏览器,并在文本框中键入表达式。在第二种情况下,我可能会模拟解析器
对象-因此我肯定是在测试我的代码而不是guice-我只是在两种情况下重用BDD接受标准