Shilpa
Shilpa

Reputation: 85

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

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

Upvotes: 1

Views: 20636

Answers (3)

You need the @RunWith(Parameterized.class) on top of the class.

If you use the @Parameters correct it will run just fine. :)

Upvotes: 2

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11950

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.

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.

public Outlook() {
    super(new FirefoxDriver());      
}

Hope this helps.

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140318

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.

Upvotes: 3

Related Questions