运行 来自命令行的 junit class 文件
Running a junit class file from command line
我从命令行尝试 运行 一个 junit class 文件,我收到以下错误:
D:\saurabh>java -cp D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore D:\saurabh\NewMercury.class
JUnit version 4.4
Could not find class: D:\saurabh\NewMercury.class
Time: 0
OK (0 tests)
D:\saurabh>dir /p
Volume in drive D has no label.
Volume Serial Number is 4C6B-63F6
Directory of D:\saurabh
06/24/2015 06:09 PM <DIR> .
06/24/2015 06:09 PM <DIR> ..
05/27/2015 04:42 PM 7,187,628 export.dat
06/24/2015 05:51 PM 3,302 NewMercury.class
03/11/2015 07:32 PM 148,696 PortQryV2.exe
class 文件存在,但仍然显示错误 "Could not find class: D:\saurabh\NewMercury.class"
D:\saurabh\NewMercury.class 无效方式.
从 D:\saurabh 目录执行的正确命令是:
D:\saurabh>java -cp .;D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
现在如果你是 运行 命令:
C:\Users2720\workspace\Test\bin\com\tests>java -cp .;D:\testing\selenium_scri
pts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
运行 如下:
C:\Users2720\workspace\Test\bin>java -cp .;D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore com.tests.NewMercury
//运行ning junit test(命令行)格式:
java -cp .;path_to_junit_jar\junit-4.4.jar org.junit.runner.JUnitCore you_class_with_package
path_to_junit_jar - D:\testing\selenium_scri分
you_class_with_package - com.tests.NewMercury 即考虑到 NewMercury.java 有一个声明 "package com.tests"
我前进了一点,但仍然面临这个问题:
这是原始的 class 文件,它在 eclipse
中运行良好
C:\Users2720\workspace\Test\bin\com\tests>java -cp .;D:\testing\selenium_scri
pts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
JUnit version 4.4
Exception in thread "main" java.lang.NoClassDefFoundError: NewMercury (wrong nam
e: com/tests/NewMercury)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)
这是 NewMercury.java 的代码:
package com.tests;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewMercury {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.co.in";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testNewMercury() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("mercury travels");
driver.findElement(By.linkText("Mercury Travels: Flight, Cheap Air Tickets , Hotels, Holiday ...")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
我从命令行尝试 运行 一个 junit class 文件,我收到以下错误:
D:\saurabh>java -cp D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore D:\saurabh\NewMercury.class
JUnit version 4.4
Could not find class: D:\saurabh\NewMercury.class
Time: 0
OK (0 tests)
D:\saurabh>dir /p
Volume in drive D has no label.
Volume Serial Number is 4C6B-63F6
Directory of D:\saurabh
06/24/2015 06:09 PM <DIR> .
06/24/2015 06:09 PM <DIR> ..
05/27/2015 04:42 PM 7,187,628 export.dat
06/24/2015 05:51 PM 3,302 NewMercury.class
03/11/2015 07:32 PM 148,696 PortQryV2.exe
class 文件存在,但仍然显示错误 "Could not find class: D:\saurabh\NewMercury.class"
D:\saurabh\NewMercury.class 无效方式.
从 D:\saurabh 目录执行的正确命令是:
D:\saurabh>java -cp .;D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
现在如果你是 运行 命令:
C:\Users2720\workspace\Test\bin\com\tests>java -cp .;D:\testing\selenium_scri pts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
运行 如下: C:\Users2720\workspace\Test\bin>java -cp .;D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore com.tests.NewMercury
//运行ning junit test(命令行)格式:
java -cp .;path_to_junit_jar\junit-4.4.jar org.junit.runner.JUnitCore you_class_with_package
path_to_junit_jar - D:\testing\selenium_scri分
you_class_with_package - com.tests.NewMercury 即考虑到 NewMercury.java 有一个声明 "package com.tests"
我前进了一点,但仍然面临这个问题: 这是原始的 class 文件,它在 eclipse
中运行良好C:\Users2720\workspace\Test\bin\com\tests>java -cp .;D:\testing\selenium_scri
pts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury
JUnit version 4.4
Exception in thread "main" java.lang.NoClassDefFoundError: NewMercury (wrong nam
e: com/tests/NewMercury)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)
这是 NewMercury.java 的代码:
package com.tests;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewMercury {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.co.in";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testNewMercury() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("mercury travels");
driver.findElement(By.linkText("Mercury Travels: Flight, Cheap Air Tickets , Hotels, Holiday ...")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}