问题描述
我无法弄清楚如何将 JUnit 正确安装到我的 Mac 上.我知道我应该将它添加到路径环境变量中,并且我已经尝试了一些我在谷歌上找到的关于如何做到这一点的教程,但我不断收到错误.这是我使用的教程的链接: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.
任何帮助将不胜感激!
推荐答案
初步检查:首先检查您的 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 && 在你的主目录中创建一个
.然后运行 java
文件夹mkdir javacp ~/Downloads/{junit-X.Y.jar,hamcrest-core-XX.YY.jar} ~/java/
将两个 .jar 复制到那里.(相应地替换X
、Y
、XX
、YY
).[注意:如果文件夹放置不便,请不要担心,您可以在以后随时更改 jUnit 文件夹]. 编辑你的类路径.我们现在需要编辑我们的
.bash_profile
文件以将这些文件添加到我们的类路径中(如果您使用 zsh,请编辑您的.zshrc
文件).
- 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)
这篇关于需要帮助在 Mac 上安装 JUnit/如何在 Mac OSX 上将 JUnit 添加到 Path 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!