【问题标题】:Running JUnit from Java program in Eclipse在 Eclipse 中从 Java 程序运行 JUnit
【发布时间】: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 - 试过了,在我的问题本身的一个步骤中提到了这一点。

标签: java eclipse maven junit


【解决方案1】:

哇!现在可以开始工作了。这是任何人将来面临类似问题并需要详细解决方案的解决方案-

对于普通的 JAVA 文件

  • 编译它。我需要进入包含我的 java 文件 (Framework.java) 的文件夹。现在在这里,在这个文件中,我调用了其他文件中定义的方法。因此,仅编译此文件会引发错误。所以,我不得不这样做-

    javac -cp <list of all jars separated by ;> *.java
    

例如,

    javac -cp "C:\Program Files\Java\jre8\lib\ext\sunmscapi.jar";"C:\Program Files\Java\jre8\lib\ext\sunpkcs11.jar";"C:\Program Files\Java\jre8\lib\ext\zipfs.jar"; *.java
  • 运行这个。我的 Framework.java 文件位于文件夹 C:\abc\xyz\src\main\java\com\so\generic\framework\Framework.java 下,因此要确保 java 可以跟踪包 com.so.generic.framework,我向上移动了几个文件夹直到'java'并从那里打开命令提示符。然后我输入-

    java -cp "C:\Program Files\Java\jre8\lib\ext\sunpkcs11.jar";"C:\Program Files\Java\jre8\lib\ext\zipfs.jar";"G:\abc\xyz\src\main\java" com.so.generic.framework.Framework
    

请注意在类路径中添加最后一个参数“G:\abc\xyz\src\main\java”以使其工作。

用于 JUnit 测试 编译过程保持不变。但是要运行,我们需要更改最后一位。我们不是简单地写 com.so.generic.framework.Framework(在本例中为 com.so.junit.tests.ProjectSpecificTests),而是写这个-

    java -cp "C:\Program Files\Java\jre8\lib\ext\sunpkcs11.jar";"C:\Program Files\Java\jre8\lib\ext\zipfs.jar";"G:\abc\xyz\src\main\java";"G:\FrameWorkSpace_HC\GenericAutomation\src\test\java" org.junit.runner.JUnitCore com.so.junit.tests.ProjectSpecificTests

希望这对其他人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2016-02-27
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多