且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要帮助在Mac上安装JUnit /如何在Mac OSX上添加JUnit到Path环境变量

更新时间:2021-07-20 03:24:53

初步检查
首先检查您的JRE是否安装好。您应该可以打开一个终端,并键入 javac 而不会收到任何错误,并查看使用选项:

Preliminary checks: first check your JRE is installed ok. You should be able to open a terminal and type javac without getting any errors, and see the usage options:

Usage: javac <options> <source files>
where possible options include:
...

同时键入 whereis javac 将为您提供安装java编译器的路径。当我在OSX安装中键入这个,我得到 / usr / bin / javac 。你应该得到一些路径显示给你没有错误。假设全部检出,我们来安装jUnit。请按照以下步骤操作。

Also typing whereis javac will give you the path of where your java compiler is installed. When I type this on my OSX installation, I get /usr/bin/javac. You should get some path shown to you with no errors. Assuming that all checks out, let's install jUnit. Follow the steps below.


  1. 下载.jar文件。转到 https://github.com/junit-team/junit/wiki/Download-and-Install ,然后单击链接的junit.jar 。找到最新稳定版本的行,然后在下载列下单击jar链接。这将下载一个文件 junit-X.Y.jar 。重复此操作,并下载最新的稳定版本的hamcrest:下载 hamcrest-core-XX.YY.jar

  2. 创建jUnit主文件夹。我们需要创建一个文件夹,并将.jars放在这个文件夹中。我建议您通过运行 cd&&&&&&&          mkdir java 。然后运行 cp〜/ Downloads / {junit-X.Y.jar,hamcrest-core-XX.YY.jar}〜/ java / 复制两个.jars。 (替换 X Y XX YY 相应)。 [注意:如果文件夹位置变得不方便,不要担心,您可以随时更改jUnit文件夹]。

  3. 编辑您的类路径。我们现在需要编辑我们的 .bash_profile 文件将这些文件添加到我们的类路径中(如果您使用zsh编辑您的 .zshrc $ file

  1. Download the .jar files. Go to https://github.com/junit-team/junit/wiki/Download-and-Install and click the link for junit.jar. Find the row for the latest stable release then under the downloads column click 'jar' link. This will download a file junit-X.Y.jar. Repeat this again and download the latest stable release for hamcrest: download hamcrest-core-XX.YY.jar
  2. Create a jUnit Home folder. We need to create a folder and put the .jars you downloaded into this folder. I suggest creating a java folder in your home directory by running cd && mkdir java. Then running cp ~/Downloads/{junit-X.Y.jar,hamcrest-core-XX.YY.jar} ~/java/ to copy the two .jars there. (replace X, Y, XX, YY accordingly). [Note: If the folder placement becomes an inconvenience, don't worry, you can change the jUnit folder at any later date].
  3. Edit your classpath. We now need to edit our .bash_profile file to add these files to our classpath (if you are using zsh edit your .zshrc file).

export JUNIT_HOME="$HOME/java"
export PATH="$PATH:$JUNIT_HOME"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-X.Y.jar:$JUNIT_HOME/hamcrest-core-XX.YY.jar"


  • 测试它的作品。重新启动你的终端运行 echo $ CLASSPATH ,应该没有错误,抱怨文件无法找到。现在用一个简单的测试用例创建一个测试文件。在 java 文件夹中,创建一个名为 TestBasic.java 的文件:

  • Test it works. Restart your terminal. Run echo $CLASSPATH, there should be no errors that complain that files are unable to be found. Now create a test file with a trivial test case. In your java folder, create a file called TestBasic.java with:

    import junit.framework.TestCase;
    public class TestBasic extends TestCase {
      public void testTrue() {
        assertTrue(true);
      }
    }
    


  • 现在cd进入 java 目录运行 javac TestBasic.java 后跟 java org.junit .runner.JUnitCore TestBasic 。如果一切正常,那么您将得到如下输出:

    Now cd into the java directory run javac TestBasic.java followed by java org.junit.runner.JUnitCore TestBasic. If everything is ok, then you will get an output like:

    JUnit version 4.11
    .
    Time: 0.006
    
    OK (1 test)