【问题标题】:Can't find JUnit on Mac在 Mac 上找不到 JUnit
【发布时间】:2013-06-12 06:41:49
【问题描述】:

所以我正在尝试从this 教程构建一个项目。我运行蚂蚁,并得到错误。我看到的第一件事是 ...错误:包 junit.framework 不存在 [javac] 导入 junit.framework.TestCase; 该教程对可能的错误非常不具体。它只是说我可能没有安装 JUnit。我正在运行最新版本的 Java,我认为它与 JUnit 一起提供。所以问题变成了找到它(或至少确认它存在)并将其添加到构建文件中。我怎么做?

Build.xml

<project name="cobertura.examples.basic" default="coverage" basedir=".">

    <description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

    <property file="build.properties" />

    <path id="cobertura.classpath">
        <fileset dir="${cobertura.dir}">
            <include name="cobertura.jar" />
            <include name="lib/**/*.jar" />
        </fileset>
    </path>
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <target name="init">
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${instrumented.dir}" />
        <mkdir dir="${reports.xml.dir}" />
        <mkdir dir="${reports.html.dir}" />
        <mkdir dir="${coverage.xml.dir}" />
        <mkdir dir="${coverage.summaryxml.dir}" />
        <mkdir dir="${coverage.html.dir}" />
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
            <classpath refid="cobertura.classpath" />
        </javac>
    </target>

    <target name="instrument" depends="init,compile">
        <!--
            Remove the coverage data file and any old instrumentation.
        -->
        <delete file="cobertura.ser"/>
        <delete dir="${instrumented.dir}" />

        <!--
            Instrument the application classes, writing the
            instrumented classes into ${build.instrumented.dir}.
        -->
        <cobertura-instrument todir="${instrumented.dir}">
            <!--
                The following line causes instrument to ignore any
                source line containing a reference to log4j, for the
                purposes of coverage reporting.
            -->
            <ignore regex="org.apache.log4j.*" />

            <fileset dir="${classes.dir}">
                <!--
                    Instrument all the application classes, but
                    don't instrument the test classes.
                -->
                <include name="**/*.class" />
                <exclude name="**/*Test.class" />
            </fileset>
        </cobertura-instrument>
    </target>

    <target name="test" depends="init,compile">
        <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
            <!--
                Note the classpath order: instrumented classes are before the
                original (uninstrumented) classes.  This is important.
            -->
            <classpath location="${instrumented.dir}" />
            <classpath location="${classes.dir}" />

            <!--
                The instrumented classes reference classes used by the
                Cobertura runtime, so Cobertura and its dependencies
                must be on your classpath.
            -->
            <classpath refid="cobertura.classpath" />

            <formatter type="xml" />
            <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
            <batchtest todir="${reports.xml.dir}" unless="testcase">
                <fileset dir="${src.dir}">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>

        <junitreport todir="${reports.xml.dir}">
            <fileset dir="${reports.xml.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${reports.html.dir}" />
        </junitreport>
    </target>

    <target name="coverage-check">
        <cobertura-check branchrate="34" totallinerate="100" />
    </target>

    <target name="coverage-report">
        <!--
            Generate an XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
    </target>

    <target name="summary-coverage-report">
        <!--
            Generate an summary XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
    </target>

    <target name="alternate-coverage-report">
        <!--
            Generate a series of HTML files containing the coverage
            data in a user-readable form using nested source filesets.
        -->
        <cobertura-report destdir="${coverage.html.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>
    </target>

    <target name="clean" description="Remove all files created by the build/test process.">
        <delete dir="${classes.dir}" />
        <delete dir="${instrumented.dir}" />
        <delete dir="${reports.dir}" />
        <delete file="cobertura.log" />
        <delete file="cobertura.ser" />
    </target>

    <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>

</project>

【问题讨论】:

    标签: xml ant junit cobertura


    【解决方案1】:

    确实,当您获取适用于 Mac 的最新 JDK 时,您不会获得 JUnit。

    虽然从http://junit.org 下载和安装最新的 junit.jar,并配置 ant 以找到它会对您有所帮助,但您最好从在线 JUnit 页面学习 junit,而不是从一个非常简短的 cobertura 在线教程。这些文档有一个下载和安装指南,非常好(假设您知道什么是类路径),以及一个超小型的入门指南。

    您不会认为 junit.framework.TestCase 类是 JUnit 版本 3 的一部分,它现在非常旧了。除非您有遗留代码可以使用,否则您可能希望从学习 JUnit 4 开始,它已经存在很多年了。

    还有其他方法可以使用 JUnit。您的 IDE 可能已经有 JUnit 插件。另一种方法是使用 Maven。通过在您的 maven 项目文件中指定 junit,maven 将自动为您获取并安装 junit。 Maven 相当复杂,但一旦你习惯了它,你可能会将它用于所有 Java 应用程序。

    附录

    这是一个在 Mac 上使用 Maven 和 JUnit 的完整示例。您只需要自己安装 JDK 和 Maven。完成后,为新项目创建一个目录并只创建三个文件:

    • PROJECT_ROOT/pom.xml
    • PROJECT_ROOT/src/main/java/com/example/Pair.java
    • PROJECT_ROOT/src/test/java/com/example/PairTest.java

    这里是 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/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>pairs</artifactId>
      <version>1.0</version>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    

    这里是 Pair.java

    package com.example;
    class Pair {
        int x, y;
        public Pair(int x, int y) {this.x = x; this.y = y;}
        public int getX() {return x;}
        public int getY() {return y;}
        @Override public String toString() {return String.format("(%d,%d)", x, y);}
    }
    

    这里是 PairTest.java

    package com.example;
    import static org.hamcrest.CoreMatchers.is;
    import static org.junit.Assert.assertThat;
    import org.junit.Test;
    
    public class PairTest {
    
        @Test
        public void gettersWork() {
            Pair p = new Pair(4, 6);
            assertThat(p.getX(), is(4));
            assertThat(p.getY(), is(6));
            assertThat(p.toString(), is("(4,6)"));
        }
    }
    

    现在从您的项目根目录中,只需键入

    mvn test
    

    一切都应该正常。请注意,第一次运行 maven 时,可能需要大约 4 分钟来下载数百万兆字节的内容。别担心,这很正常。它会为你获取junit。

    您应该看到的最后几行类似于:

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.example.PairTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.308 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    

    希望对您有所帮助。

    【讨论】:

    • 我更关心的是如何尝试学习 Cobertura、Jekins 和 JUnit 测试如何协同工作,而不是 JUnit 本身。但是你的回答给了我很多思考,所以如果你不介意我有几个新问题要问你。 1.如果我得到了JUnit4,我该如何配置ant来找到它? 2. 我真的不知道 wat classpath 是什么,需要详细说明吗? 3. 有没有和我用的类似的 Maven 教程,里面有项目布局,junit 测试等等?
    • 没问题。我在答案中添加了一个完整的 Maven 示例。我已经好几年没用过 ant 了,我在 2004 年搬到了 maven。
    • 首先非常感谢您花时间编写示例。其次,我正在运行它,但此时我得到“没有要运行的测试”。我不确定为什么我认为我的 JUnit 罐子可能有问题以及它们在哪里。第三次运行mcn后,目标目录是干什么用的?
    • 如果您看到“没有要运行的测试”,请确保文件位于完全正确的目录中并且包名称完全正确。 @Test 注释也是必要的。 Maven 让您将所有源代码放在src 目录中,并将其所有输出(例如,.class 文件、jar 和报告)写入target 目录。这个想法是src 中的所有内容都将进入版本控制,target 中的所有内容都只是输出。不过Here is some more info 不是针对初学者的。
    猜你喜欢
    • 2011-11-09
    • 2015-05-19
    • 2021-02-19
    • 2016-07-19
    • 2020-07-02
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多