java.lang.Exception: 测试类应该只有一个公共零参数构造函数:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34397700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.Exception: Test class should have exactly one public zero-argument constructor:
提问by Shilpa
I have a class : Function Library where I am instantiating the webdriver instance in constructor as below
我有一个类:函数库,我在构造函数中实例化 webdriver 实例,如下所示
public class FunctionLibrary {
public WebDriver driver;
public FunctionLibrary(WebDriver driver)
{
driver = new FirefoxDriver();
this.driver=driver;
}
public WebDriver getDriver(){
return this.driver;
}
}
I am accessing the webdriver instance in child class extending super class: function library
我正在访问扩展超类的子类中的 webdriver 实例:函数库
public class Outlook extends FunctionLibrary{
public Outlook(WebDriver driver) {
super(driver);
}
@Before
public void testSetUp()
{
getDriver().navigate().to("https://google.com");
}
@After
public void closeTest()
{
getDriver().close();
}
@Test
public void openOutlookAndCountTheNumberOfMails()
{
System.out.println("executed @Test Annotation");
}
}
when I execute the above piece of junit code I am getting error as
当我执行上面一段 junit 代码时,我收到错误消息
java.lang.Exception: Test class should have exactly one public zero-argument constructor
java.lang.Exception: 测试类应该只有一个公共零参数构造函数
Anybody can let me where I am going wrong
任何人都可以让我哪里出错了
回答by Andy Turner
There is no need to pass a parameter into the ctor of FunctionLibrary
, since you simply overwrite its value:
无需将参数传递给 的构造函数FunctionLibrary
,因为您只需覆盖其值:
public class FunctionLibrary {
public WebDriver driver;
public FunctionLibrary()
{
this.driver=new FirefoxDriver();
}
// etc.
}
Making this change means you don't need to pass a parameter in from the test class: just remove its constructor.
进行此更改意味着您不需要从测试类中传入参数:只需删除其构造函数即可。
回答by Athinodoros Sgouromallis
You need the @RunWith(Parameterized.class)
on top of the class.
你需要@RunWith(Parameterized.class)
在班级之上。
If you use the @Parameters
correct it will run just fine. :)
如果您使用@Parameters
正确,它将运行得很好。:)
回答by Sri Harsha Chilakapati
You do not have a public zero-arg constructor. The testing environment doesn't know what to pass to it's constructor, since you require a WebDriver
object to be passed.
您没有公共零参数构造函数。测试环境不知道向它的构造函数传递什么,因为您需要WebDriver
传递一个对象。
public Outlook(WebDriver driver) {
super(driver);
}
There, what do you want the testing environment to pass? Instead do something like keep a zero arg constructor, and pass in a WebDriver instance yourself. Something like this should work.
在那里,您希望测试环境通过什么?而是做一些类似保持零参数构造函数的事情,并自己传入一个 WebDriver 实例。像这样的事情应该有效。
public Outlook() {
super(new FirefoxDriver());
}
Hope this helps.
希望这可以帮助。