需要帮助在Mac上安装JUnit /如何在Mac OSX上添加JUnit到Path环境变量
我无法弄清楚如何将JUnit正确安装到我的Mac上。我知道我应该将其添加到路径环境变量中,我已经尝试了一些我在Google上发现的教程,如何做到这一点,但是我不断收到错误。
这是我使用的教程的链接:
http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac-os-x
我有一种感觉,我在步骤3中做错了事情。我已经将junit.jar文件放在Library文件夹中。
I am not able to figure out how to correctly install JUnit onto my mac. I know I am supposed to add it to the path environmental variable, and I have tried a few tutorials I've found on Google on how to do that, but I keep getting errors. This is the link to the tutorial I have used: http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac-os-x I have a feeling I am doing something wrong in step 3. I have placed the junit.jar file in the Library folder by the way.
任何帮助将不胜感激!
Any help will be greatly appreciated!
初步检查:
首先检查您的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.
- 下载.jar文件。转到 https://github.com/junit-team/junit/wiki/Download-and-Install ,然后单击链接
的junit.jar
。找到最新稳定版本的行,然后在下载列下单击jar链接。这将下载一个文件junit-X.Y.jar
。重复此操作,并下载最新的稳定版本的hamcrest:下载hamcrest-core-XX.YY.jar
- 创建jUnit主文件夹。我们需要创建一个文件夹,并将.jars放在这个文件夹中。我建议您通过运行
cd&&&&&&& mkdir java
。然后运行cp〜/ Downloads / {junit-X.Y.jar,hamcrest-core-XX.YY.jar}〜/ java /
复制两个.jars。 (替换X
,Y
,XX
,YY
相应)。 [注意:如果文件夹位置变得不方便,不要担心,您可以随时更改jUnit文件夹]。 -
编辑您的类路径。我们现在需要编辑我们的
.bash_profile
文件将这些文件添加到我们的类路径中(如果您使用zsh编辑您的.zshrc
$ file
-
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 filejunit-X.Y.jar
. Repeat this again and download the latest stable release for hamcrest: downloadhamcrest-core-XX.YY.jar
-
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 runningcd && mkdir java
. Then runningcp ~/Downloads/{junit-X.Y.jar,hamcrest-core-XX.YY.jar} ~/java/
to copy the two .jars there. (replaceX
,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]. 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)