从命令行运行junit类文件

时间:2015-06-25 11:38:07

标签: java selenium junit4

我尝试从命令行运行junit类文件,我收到以下错误:

 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

类文件存在,但仍然显示错误&#34;找不到类:D:\ saurabh \ NewMercury.class&#34;

3 个答案:

答案 0 :(得分:2)

D:\ saurabh \ NewMercury.class是无效的方式。

从D:\ saurabh dir执行的正确命令是:

D:\saurabh>java -cp .;D:\testing\selenium_scripts\junit-4.4.jar org.junit.runner.JUnitCore NewMercury

现在,如果您正在运行命令:

C:\ Users \ 352720 \ workspace \ Test \ bin \ com \ tests&gt; java -cp .; D:\ testing \ selenium_scri pts \ junit-4.4.jar org.junit.runner.JUnitCore NewMercury

按以下方式运行: C:\ Users \ 352720 \ workspace \ Test \ bin&gt; java -cp .; D:\ testing \ selenium_scripts \ junit-4.4.jar org.junit.runner.JUnitCore com.tests.NewMercury

//运行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_scripts

  • you_class_with_package - com.tests.NewMercury即考虑NewMercury.java有声明“package com.tests”

答案 1 :(得分:0)

我前进了一步,但仍面临这个问题: 这是原始的类文件,它可以在eclipse中正常工作

C:\Users\352720\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.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.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)

答案 2 :(得分:0)

这是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;
    }
  }
}