2016-11-15 42 views
0

我下面的JUnit測試java.lang.Exception的:測試類應該有且僅有一個公共構造

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. 

回答

2
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; 
} 
+0

確切地說,這就像錯誤說:**測試類應該只有一個公共構造函數**所以必須有一個公共構造函數....有時候peope提問可以用「請閱讀你的控制檯的輸出「。因爲在我看來這不是一個好問題(答案已經由輸出結果給出),我低估了這個問題,並提出了答案。 –

0

在編寫JUnit時要記住的一點是,雖然它在執行測試方法之前調用測試類的構造函數。你可以通過在構造函數本身中添加一個System.out.println消息來驗證它

在你的代碼中,你已經將你的構造函數定義爲私有的,所以測試框架將不能實例化你的測試類來執行測試方法。範圍向公衆。

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