【发布时间】:2016-03-04 02:47:50
【问题描述】:
我目前正在使用 cucumber、JUnit 和 Selenium 开发一个 java 测试框架。我已经参与过类似的项目,但我在这个项目上遇到了问题。
我正在尝试创建一个单例的 Context 类。我想使用 cucumber-picocontainer 让这个类在每个步骤定义类中都可以访问。我在我的 pom.xml 中添加了依赖项,但是每次我尝试执行测试时,都会出现一个异常:“NewLoginSteps 没有空构造函数。如果需要 DI,请将 cucumber-picocontainer 放在类路径中” .我尝试手动导入 jar,但没有帮助。
这是我的配置示例:
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <properties> <cucumber.version>1.2.4</cucumber.version> <selenium-java.version>2.39.0</selenium-java.version> </properties> <dependencies> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-picocontainer</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </project>
TestContext.java:
public class TestContext {
private static Map<String, String> siteLocations = new HashMap<String, String>();
private static boolean initialized = false;
private static boolean firstInitDone = false;
private static WebDriver driver;
private static boolean testsToRun = false;
private static AutomatedTestMode modeAsEnum;
@Before
public void setUp(Scenario scenario) {
initialize();
Log.startTestCase(scenario.getName());
afterAll();
}
....
}
一个步骤定义类:
public class NewLoginSteps extends NewSuperSteps {
public NewLoginSteps(TestContext context){
super(context);
}
@When("^I log in nova as \"([^\"]*)\" user with \"([^\"]*)\" \"([^\"]*)\"$")
public void newLogin(String instance, String username, String password){
Assert.assertEquals(true, false);
}
@Then("^The user is connected$")
public void The_user_is_connected(){
throw new PendingException();
}
}
我的 superSepts 课程:
public class NewSuperSteps {
protected TestContext context;
public NewSuperSteps(TestContext context){
this.context=context;
}
}
你知道我做错了什么吗?我已经使用 picocontainer 来做同样的事情并且它正在工作。
【问题讨论】:
-
我遇到了类似的问题,你有没有找到解决这个问题的方法?
-
@himawan_r 不,我的问题当时没有解决。从那以后我改变了很多东西。而且我不再使用 picocontainer
-
我遵循 Vacha Dave 的解决方案,使所有 info cukes 版本相同,它适用于我 :)
标签: java maven selenium cucumber picocontainer