在命令行上运行Junit

时间:2019-02-06 04:16:18

标签: junit command-line

在Windows命令提示符下运行initializationError(org.junit.runner.JUnitCommandLineParseResult)代码时,出现

jUnit错误:

Z:\ lib \ com \ example \ tests> java -cp Z:\ lib \ junit-4.12.jar; Z:\ lib \ hamcrest-core-1.3.jar org.junit.runner.JUnitCore TripPlannerJunit     JUnit版本4.12.E     时间:0     有1个失败:     1)初始化错误(org.junit.runner.JUnitCommandLineParseResult)     java.lang.IllegalArgumentException:在org.junit.runner.JUnitCommandLineParseResult.java:102上的org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)上找不到类[TripPlannerJunit] at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)上的junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)org.junit.runner.JUnitCore.main(JUnitCore.java:36) 引起原因:java.lang.ClassNotFoundException:TripPlannerJunitat java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)at java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java :178)在java.base / java.lang.ClassLoader.loadClass(ClassLoader.java:521) 在org.junit.internal.Classes.getClass(Classes.java :)在java.base / java.lang.Class.forName0(本机方法)在java.base / java.lang.Class.forName(Class.java:398) 16)位于org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)...另外4个     失败!!!     测试运行:1,失败:1 编辑-。结构如下: 所有的罐子都在“ Z:\ TripPlanner \ lib”中。 类和Java文件位于-“ Z:\ TripPlanner \ lib \ com \ example \ tests” 声明的包是com.example.tests

package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.ChromeDriverManager;

public class TripPlannerJUnit {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    ChromeDriverManager.getInstance().setup();
    driver = new ChromeDriver();
    baseUrl = "https://www.google.com.au/.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitledTestCase() throws Exception {
    driver.get("https://transportnsw.info/trip#/");
    driver.findElement(By.id("search-input-From")).click();
    driver.findElement(By.id("search-input-From")).clear();
    driver.findElement(By.id("search-input-From")).sendKeys("North");
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='North Sydney Station'])[1]/following::li[1]")).click();
    driver.findElement(By.id("search-input-To")).click();
    driver.findElement(By.id("search-input-To")).clear();
    driver.findElement(By.id("search-input-To")).sendKeys("Town");
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Town Hall Station'])[1]/following::ul[1]")).click();
    driver.findElement(By.id("search-button")).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;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

请指定TripPlannerJunit所在的路径。

如果这是项目的当前文件夹:

java -cp .;Z:\lib\junit-4.12.jar;...

java -cp %CD%;Z:\lib\junit-4.12.jar;...

更新(基于注释中的说明)。

假设您具有以下结构:

Z:\TripPlanner\lib\
|   hamcrest-core-1.3.jar
|   junit-4.12.jar
|
+---com
|   \---example
|       \---tests
|               TripPlannerJunit.java
|               TripPlannerJunit.class

Z:\TripPlanner\lib文件夹运行测试:

Z:\TripPlanner\lib>java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore "com.example.tests.TripPlannerJunit"