Pooja
Pooja

Reputation: 51

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

I've following junit test

package test.java.com.unit;
import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
import com.saucelabs.junit.Parallelized;
import com.saucelabs.junit.SauceOnDemandTestWatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.LinkedList;

import static org.junit.Assert.assertEquals;

@RunWith(Parallelized.class)
public class WebDriverTest implements SauceOnDemandSessionIdProvider {


public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication("USER_NAME", "ACCESS_KEY");

private String browser;
private String os;
private String version;

public @Rule SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);

public @Rule TestName testName = new TestName();

private WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Parameterized.Parameters
public static LinkedList browsersStrings() throws Exception {
LinkedList browsers = new LinkedList();
 browsers.add(new String[]{Platform.XP.toString(), "26", "firefox"});
 browsers.add(new String[]{Platform.WIN8.toString(), "10", "internet explorer"});
  browsers.add(new String[]{Platform.MAC.toString(), "5", "safari"});
 browsers.add(new String[]{Platform.MAC.toString(), "7", "iphone"});
browsers.add(new String[]{Platform.LINUX.toString(), "30", "chrome"});
 browsers.add(new String[]{Platform.ANDROID.toString(), "4.0", "android"});
return browsers;
}
private WebDriver driver;
private String sessionId;

@Before
public void setUp() throws Exception {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM,  Platform.valueOf(os));

if(browser.equalsIgnoreCase("android"))
{
 capabilities.setCapability("device-type", "tablet");
  capabilities.setCapability("device-orientation", "portrait");
}
else if(browser.equalsIgnoreCase("iphone"))
{
  capabilities.setCapability("device-orientation", "portrait");
}

String name = testName.getMethodName() + ": "+ browser+ " "+ version+ " "+ Platform.valueOf(os);
capabilities.setCapability("name",name);
this.driver= new RemoteWebDriver(new URL("http://"+ authentication.getUsername() + ":"+ authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"),capabilities);
this.sessionId= ((RemoteWebDriver)driver).getSessionId().toString();

}

@Override
public String getSessionId() {
return sessionId;
}

@Test
public void webDriver() throws Exception {
// Simple test -just go to the site and verify the title.
 driver.get("https://www.google.com/");
 assertEquals("To Do List", driver.getTitle());
}

@After
public void tearDown() throws Exception {
 driver.quit(); }   }

which is throwing

java.lang.Exception: Test class should have exactly one public constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateOnlyOneConstructor(BlockJUnit4ClassRunner.java:158)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateConstructor(BlockJUnit4ClassRunnerWithParameters.java:88)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
    at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
    at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
    at com.saucelabs.junit.Parallelized.<init>(Parallelized.java:88)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

exception while running test. Can someone help finding the solution.

Upvotes: 3

Views: 12436

Answers (4)

Dhanashree K.
Dhanashree K.

Reputation: 39

Test class must be public or if it has a constructor, it must be public for the wiring to work.

Upvotes: 0

Greg
Greg

Reputation: 2617

I got this error while trying to run the unit test in Eclipse, and it turned out that the problem was Eclipse. Running it from the command line using gradle returned a different error - that another unit test was invalid. Fixing the other invalid test fixed this error. Thanks a lot, Eclipse!

Upvotes: 0

mhasan
mhasan

Reputation: 3709

Point to rememeber while writing JUnit is that while execution it calls constructor of test class before executing test method. You can verify it by putting a System.out.println message in constructor itself

Here in your code you have defined your constructor as private so the test framework will not be able to instantiate your test class for test methods execution change the scope to public.

public WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

Upvotes: 0

aorticDefiance
aorticDefiance

Reputation: 125

private WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

should be

public WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

Upvotes: 5

Related Questions