java.lang.Exception: Test class should have exactly one public zero-argument constructor:

Shilpa picture Shilpa · Dec 21, 2015 · Viewed 16.3k times · Source

I have a class : Function Library where I am instantiating the webdriver instance in constructor as below

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

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

java.lang.Exception: Test class should have exactly one public zero-argument constructor

Anybody can let me where I am going wrong

Answer

Andy Turner picture Andy Turner · Dec 21, 2015

There is no need to pass a parameter into the ctor of FunctionLibrary, since you simply overwrite its value:

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.