Java 需要帮助在 Mac 上安装 JUnit/如何将 JUnit 添加到 Mac OSX 上的 Path 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21369953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need help installing JUnit on Mac/How to add JUnit to Path environmental variable on Mac OSX
提问by user3238564
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-xI 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.
我无法弄清楚如何将 JUnit 正确安装到我的 Mac 上。我知道我应该将它添加到路径环境变量中,我已经尝试了一些我在谷歌上找到的关于如何做到这一点的教程,但我不断收到错误消息。这是我使用过的教程的链接:http: //hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac-os-x我有一种感觉在第 3 步中做错了。顺便说一下,我已将 junit.jar 文件放在 Library 文件夹中。
Any help will be greatly appreciated!
任何帮助将不胜感激!
回答by JB Nizet
JUnit doesn't have to be installed. It's a Java library, like all the other libraries, that you can use in your code. The PATH must not be changed to use it. You just need it in the classpath, like all the other Java libraries:
不必安装 JUnit。它是一个 Java 库,就像所有其他库一样,您可以在代码中使用它。不得更改 PATH 以使用它。您只需要在类路径中使用它,就像所有其他 Java 库一样:
javac -cp junit.jar ...
to compile, and
编译,和
java -cp junit.jar ...
to run.
跑步。
回答by James Lawson
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:
初步检查:首先检查您的 JRE 是否安装好。您应该能够打开终端并键入javac
而不会出现任何错误,并查看使用选项:
Usage: javac <options> <source files>
where possible options include:
...
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.
键入whereis javac
还会为您提供安装 java 编译器的路径。当我在我的 OSX 安装上输入这个时,我得到/usr/bin/javac
. 您应该会看到一些没有错误的路径。假设所有检查都通过,让我们安装 jUnit。请按照以下步骤操作。
- Download the .jar files.Go to https://github.com/junit-team/junit/wiki/Download-and-Installand 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"
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 yourjava
folder, create a file calledTestBasic.java
with:import junit.framework.TestCase; public class TestBasic extends TestCase { public void testTrue() { assertTrue(true); } }
- 下载 .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 文件放入该文件夹中。我建议
java
通过运行在您的主目录中创建一个文件夹cd && mkdir java
。然后运行cp ~/Downloads/{junit-X.Y.jar,hamcrest-core-XX.YY.jar} ~/java/
以将两个 .jar 复制到那里。(相应地替换X
,Y
,XX
,YY
)。[注意:如果文件夹放置变得不方便,请不要担心,您可以稍后更改 jUnit 文件夹]。 编辑您的类路径。我们现在需要编辑我们的
.bash_profile
文件以将这些文件添加到我们的类路径中(如果您使用 zsh 编辑您的.zshrc
文件)。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
:import junit.framework.TestCase; public class TestBasic extends TestCase { public void testTrue() { assertTrue(true); } }
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:
现在 cd 进入java
运行目录,javac TestBasic.java
然后是java org.junit.runner.JUnitCore TestBasic
. 如果一切正常,您将得到如下输出:
JUnit version 4.11
.
Time: 0.006
OK (1 test)
回答by nav_grl_onrails
Creating a .bash_profile did not work for me, but the following commands did, after following steps 1 and 2 of James's Lawson's post above.
创建 .bash_profile 对我不起作用,但在遵循上面 James's Lawson 帖子的第 1 步和第 2 步之后,以下命令起作用了。
javac -cp .:junit-X.Y.jar TestBasic.java
followed by
其次是
java -cp .:junit-4.XX.jar:hamcrest-core-XX.YY.jar org.junit.runner.JUnitCore TestBasic
回答by tech11
- Unzip the downloaded junit file "junit4.11" (whichever version you are using)
- Open terminal
Type command:
open .bash_profile
A textfile will be opened: Paste this command in it
CLASSPATH=$CLASSPATH:"path where junit4.11 is saved"/\junit4.11/junit-4.11.jar:"path where junit4.11 is saved"/\junit4.11/junit-dep-4.11.jar export CLASSPATH
- 解压下载的 junit 文件“junit4.11”(无论您使用的是哪个版本)
- 打开终端
键入命令:
打开 .bash_profile
将打开一个文本文件:在其中粘贴此命令
CLASSPATH=$CLASSPATH:"junit4.11 保存路径"/\junit4.11/junit-4.11.jar:"junit4.11 保存路径"/\junit4.11/junit-dep-4.11.jar export CLASSPATH
save the textfile and close it.
保存文本文件并关闭它。
Come back to the terminal and type commands:
cd /"path where junit4.11 is saved"/junit4.11 java org.junit.runner.JUnitCore org.junit.tests.AllTests
回到终端并输入命令:
cd /"保存junit4.11的路径"/junit4.11 java org.junit.runner.JUnitCore org.junit.tests.AllTests
You should see the success message.
您应该会看到成功消息。
回答by supertramp
I did not have to add it to path. I downloaded the file and added it manually to my libraries.
我不必将它添加到路径中。我下载了该文件并将其手动添加到我的库中。
If you are using IntelliJ IDEA go to the root folder of your project > Open Modules and Settings > Libraries > New Project Library > Java > and select where your junit file is located.
如果您使用 IntelliJ IDEA,请转到项目的根文件夹 > 打开模块和设置 > 库 > 新建项目库 > Java > 并选择您的 junit 文件所在的位置。