java.lang.Exception:测试类应该只有一个公共构造函数
Posted
技术标签:
【中文标题】java.lang.Exception:测试类应该只有一个公共构造函数【英文标题】:java.lang.Exception: Test class should have exactly one public constructor 【发布时间】:2017-03-29 01:26:20 【问题描述】:我正在关注junit测试
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();
这是在扔
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.
【问题讨论】:
【参考方案1】:private WebDriverTest(String os, String version, String browser)
super();
this.os= os;
this.version= version;
this.browser= browser;
应该是
public WebDriverTest(String os, String version, String browser)
super();
this.os= os;
this.version= version;
this.browser= browser;
【讨论】:
正是这个,就像错误说的那样:测试类应该只有一个公共构造函数所以必须有一个公共构造函数....有时可以回答提问的人使用“请阅读控制台的输出”。因为在我看来这不是一个好问题(答案已经由输出给出),所以我对这个问题投了反对票,对答案投了赞成票。【参考方案2】:在编写JUnit时要记住的是,在执行时它会在执行测试方法之前调用测试类的构造函数。您可以通过将 System.out.println 消息放入构造函数本身来验证它
在您的代码中,您已将构造函数定义为私有,因此测试框架将无法实例化您的测试类以执行测试方法,将范围更改为公共。
public WebDriverTest(String os, String version, String browser)
super();
this.os= os;
this.version= version;
this.browser= browser;
【讨论】:
【参考方案3】:我在尝试在 Eclipse 中运行单元测试时遇到此错误,结果发现问题出在 Eclipse。使用 gradle 从命令行运行它会返回一个不同的错误 - another 单元测试无效。修复其他无效测试修复了此错误。非常感谢,Eclipse!
【讨论】:
以上是关于java.lang.Exception:测试类应该只有一个公共构造函数的主要内容,如果未能解决你的问题,请参考以下文章
使用java.lang.Exception的错误:测试类应该只有一个公共构造函数
java.lang.Exception:没有找到匹配使用 Intellij IDEA 的方法的测试
SpringBoot Junit 单元测试,提示:java.lang.Exception: No tests found matching