【发布时间】:2014-06-19 09:58:26
【问题描述】:
我是第一次使用 JUnit,我被要求做的是通过从 java 程序中“调用”以编程方式运行 Junit 测试。 (所有这些都在 Eclipse 中) 请注意,右键单击包含 Junit 测试的文件并选择“运行为 Junit 测试”可以正常工作。下面是我在eclipse中的项目结构(由于我是新用户无法粘贴图片,所以提供链接)-
http://postimg.org/image/gplfswqm1/
我已经使用 maven 来获取所需的所有 jars。我也在粘贴 pom.xml,以防万一结果是相关的-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.so.generic.automation</groupId>
<artifactId>GenericAutomation</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.version>2.3.2</maven.compiler.version>
<selenium.version>2.39.0</selenium.version>
<junit.version>4.11</junit.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java</directory>
</testResource>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.resources</artifactId>
<version>3.7.100</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>3.3.0-v_771</version>
</dependency>
</dependencies>
</project>
在 src/main/java 中,我在文件 Framework.java 中有我的 Java 程序 在 src/test/java 中,我有名为 ProjectSpecificTests.java 的 Junit 测试
我需要做的是从 Framework.java 内部以某种方式运行这个 ProjectSpecificTests.java。我尝试从 Java 代码启动命令提示符并调用 java -cp .....,但我无法非常清楚地理解应该在那里传递哪些参数,因为我不断收到 NoClassDefFoundException、ClassNotFoundException 等错误。如果我明确提到罐子的位置,比如这里-
javac -cp "C:\\Users\\himanshuc\\.jenkins\\war\\WEB-INF\\lib\\hamcrest-core-1.3.jar";"C:\\Users\\himanshuc\\.m2\\repository\\org\\seleniumhq\\selenium\\selenium-firefox-driver\\2.42.0";"C:\\Users\\himanshuc\\.m2\\repository\\org\\seleniumhq\\selenium\\selenium-api\\2.42.0";"C:\\Users\\himanshuc\\.m2\\repository\\org\\seleniumhq\\selenium\\selenium-parent\\2.42.0";"C:\\Users\\himanshuc\\.m2\\repository\\org\\seleniumhq\\selenium\\selenium-remote-driver\\2.42.0";"C:\\Users\\himanshuc\\.m2\\repository\\org\\seleniumhq\\selenium\\selenium-support\\2.42.0";"C:\\Users\\himanshuc\\.m2\\repository\\junit\\junit\\4.11\\junit-4.11.jar" org.junit.runner.JUnitCore ProjectSpecificTests.java
我从文件夹中运行了这个-
G:\FrameWorkSpace_HC\GenericAutomation\src\test\java\com\so\junit\tests
然后抛出新的错误消息,比如-
ProjectSpecificTests.java:8: error: package com.so.generic.framework does not ex
ist
import com.so.generic.framework.Framework;
^
ProjectSpecificTests.java:12: error: cannot find symbol
static Framework objFramework;
^
symbol: class Framework
location: class ProjectSpecificTests
error: Class names, 'org.junit.runner.JUnitCore', are only accepted if annotatio
n processing is explicitly requested
ProjectSpecificTests.java:18: error: cannot find symbol
objFramework = new Framework();
^
symbol: class Framework
location: class ProjectSpecificTests
4 errors
谁能指导我在这里缺少什么?我对任何能达到最终结果的解决方案都很好,不一定是命令提示符。
如果问题描述太长,我很抱歉。这是我第一次尝试在这个论坛上提问。如有错误请见谅。
【问题讨论】:
-
把 junit.jar 库放到你的类路径中
-
这有点奇怪?当您尝试运行测试源中的 ProjectSpecificTests 时,Framework.java 位于您的应用程序源 (src/main/java) 中。项目本身的结构看起来不错。为什么需要从生产代码运行测试?或者它只是您需要的测试的一部分?还是您想在软件中执行测试?
-
其实我的要求差不多。在那里不能做太多事情。 src/main/java/Framework.java 中的代码在未来将几乎保持不变,Junit 测试将在 src/test/java/ProjectSpecificTests.java 中,它会不时地变化。因此,您可以将 Framework.java 视为包含这些测试将使用的通用方法的文件。所有这些基本上都可以正常工作并且非常稳定。但是新的要求表明这将全部从命令行启动,而不是右键单击->Run as Junit Test
-
@AurA - 试过了,在我的问题本身的一个步骤中提到了这一点。