(Java / Junit)我设置了类路径,现在我无法在命令提示符下编译测试文件

时间:2012-10-18 00:45:57

标签: java junit classpath

好的,我正在尝试使用import junit.framework.*;测试一个带有测试类的类,然后下载Junit库并使用类路径保存它:C:\Program Files (x86)\Java\jdk1.7.0_07\lib\junit-4.8.2.j…

我设置了CLASSPATH,每次我尝试编译测试文件时都会收到此错误:

C:\Users\Anita\Documents\java\Project2>j… TestImage.java
TestImage.java:1: error: package junit.framework does not exist
import junit.framework.*;
^

我在classpath做错了吗?我尝试了一切!关于如何最终让junit工作的任何建议?

2 个答案:

答案 0 :(得分:1)

尝试解压缩jar以检查内容是否真的存在。 另外,设置环境变量,尝试将类路径作为参数传递,如下所示:

javac -cp ./junit-4.8.2.jar TestImage.java 

但不是./junit-4.8.2.jar,而是使用jar的完整路径。

答案 1 :(得分:1)

......迟了几年......

至少在2014年,您需要import org.junitJUnit "Getting Started" guide状态"不要在junit.framework或junit.extensions中使用任何类。"

举个例子:

import org.junit.*;

public class TestFoobar {
    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testSomething() {
    }

    @Test
    @Ignore
    public void thisIsIgnored() {
    }

    @After
    public void tearDown() throws Exception {
        // Code executed after each test
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
}

...然后运行测试

C:\junit>dir
 Volume in drive C is L032336
 Volume Serial Number is CAB2-E609

 Directory of C:\junit

2014-10-20  06:57    <DIR>          .
2014-10-20  06:57    <DIR>          ..
2014-10-20  06:11            45,024 hamcrest-core-1.3.jar
2014-10-20  06:11           245,039 junit-4.11.jar
2014-10-20  06:57               518 TestFoobar.java
               3 File(s)        290,581 bytes
               2 Dir(s)  277,498,945,536 bytes free

C:\junit>javac -cp junit-4.11.jar TestFoobar.java

C:\junit>java -cp .;junit-4.11.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestFoobar
JUnit version 4.11
I.
Time: 0.007

OK (1 test)