java 在 Junit-4.11 中找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17397735/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:52:38  来源:igfitidea点击:

Could not find class in Junit-4.11

javajunit

提问by lpy

I configured Junit-4.11 on my Mac, compile with javachas no error, but when I run with java, I got Could not find class: HelloWorldTest

我在我的 Mac 上配置了 Junit-4.11,编译javac没有错误,但是当我运行时java,我得到了Could not find class: HelloWorldTest

Here is my HelloWorld.javaand HelloWorldTest.java

这是我的HelloWorld.javaHelloWorldTest.java

import java.util.*;

public class HelloWorld {
    public String output() {
        return "Hello world!";
    }
}

import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.Test;
import java.util.*;
import org.junit.*;

public class HelloWorldTest {
    public HelloWorld helloworld = new HelloWorld();

    @BeforeClass
    public static void oneTimeSetUp() {
        System.out.println("@BeforeClass - oneTimeSetUp");
    }
    @AfterClass
    public static void oneTimeTearDown() {
        System.out.println("@AfterClass - oneTimeTearDown");
    }
    @Before
    public void setUp() {
        System.out.println("@Before - setUp");
    }
    @After
    public void tearDown() {
        System.out.println("@After - tearDown");
    }
    @Test
    public void testOutput() {
        assertEquals(helloworld.output(), "Hello world!");
        System.out.println("@Test - testOutput");
    }
}

And I run with

我跑了

javac -classpath ./ HelloWorldTest.java

javac -classpath ./ HelloWorldTest.java

and java -classpath ./ org.junit.runner.JUnitCore HelloWorldTest

java -classpath ./ org.junit.runner.JUnitCore HelloWorldTest

What I got is

我得到的是

JUnit version 4.11
Could not find class: HelloWorldTest

Time: 0.002

OK (0 tests)

I put junit-4.11.jarin the current directory with HelloWorld.javaand HelloWorldTest.java, I also put it in /Library/Java/Extensions

junit-4.11.jarHelloWorld.javaand放在当前目录HelloWorldTest.java,我也把它放在 /Library/Java/Extensions

What I tried to solve is to set JAVA_HOMEand CLASSPATH, but it did not work.

我试图解决的是设置JAVA_HOMECLASSPATH,但它没有用。

Could someone point out what was going wrong? I was really confused.

有人可以指出出了什么问题吗?我真的很困惑。

Thankyou.

谢谢你。

Well I have solved my problem with the following steps. My Mac is Mac OSX 10.8 and I used JVM-1.6 provided by Apple. You can download it by clicking here.

好吧,我已经通过以下步骤解决了我的问题。我的 Mac 是 Mac OSX 10.8,我使用了 Apple 提供的 JVM-1.6。你可以点击这里下载。

  1. Delete the CLASSPATHin my .zshrcfile(If you are using BashI think it is .bashrc)
  2. Delete the JUnit-4.11.jar(or any version you use) in the /Library/Java/Extensionsand any System Directory you put it in.
  3. Try to compile again and run it.
  1. 删除CLASSPATH我的.zshrc文件中的(如果您正在使用Bash我认为是.bashrc
  2. 删除JUnit-4.11.jar(或您使用的任何版本)/Library/Java/Extensions和您放入的任何系统目录。
  3. 尝试再次编译并运行它。

And I set JAVA_HOMEas /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

我设置JAVA_HOME/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

Thank you.

谢谢你。

回答by

Update: The original poster's solution is buried in a comment below. It has to do with not having the junit jar files in /Library/Java/Extensions, and not having a CLASSPATH:

更新:原始海报的解决方案隐藏在下面的评论中。它与没有 junit jar 文件有关/Library/Java/Extensions,并且没有CLASSPATH

I deleted CLASSPATH in my .zshrc file, and I also deleted junit-4.11.jar in /Library/Java/Extensions and /Library/Java/Home/lib/ext, and then JUnit-4.11 worked.

我删除了 .zshrc 文件中的 CLASSPATH,还删除了 /Library/Java/Extensions 和 /Library/Java/Home/lib/ext 中的 junit-4.11.jar,然后 JUnit-4.11 工作了。



In a temp dir (java files from your question):

在临时目录中(来自您问题的 java 文件):

HelloWorld.java
HelloWorldTest.java
junit-4.11.jar
hamcrest-core-1.3.jar

Then:

然后:

  • javac -cp junit-4.11.jar *.java
  • java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore HelloWorldTest
  • javac -cp junit-4.11.jar *.java
  • java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore HelloWorldTest

Output:

输出:

HelloWorldTest
JUnit version 4.11
@BeforeClass - oneTimeSetUp
.@Before - setUp
@Test - testOutput
@After - tearDown
@AfterClass - oneTimeTearDown

Time: 0,004

OK (1 test)

I would advise retrying from scratch

我建议从头开始重试

回答by Himanshu Bhardwaj

Ok I tested the same. Will provide the steps for the same:

好的,我测试了相同的。将提供相同的步骤:

/
|
|--HelloWorld.java
|--HelloWorldTest.java

First process HelloWorld.java:

第一个进程HelloWorld.java

javac HelloWorld.java

This will result in HelloWorld.class in the same folder.

这将导致 HelloWorld.class 位于同一文件夹中。

Next process HelloWorldTest.java:

下一个进程HelloWorldTest.java

javac -classpath C:\Himanshu_Work\repo\junit\junit.10\junit-4.10.jar;. HelloWorldTest.java

This will result in HelloWorldTest.classin the same folder.

这将导致HelloWorldTest.class位于同一文件夹中。

java -classpath C:\Himanshu_Work\repo\junit\junit.10\junit-4.10.jar;. org.junit.runner.JUnitCore  HelloWorldTest
JUnit version 4.10
@BeforeClass - oneTimeSetUp
.@Before - setUp
@Test - testOutput
@After - tearDown
@AfterClass - oneTimeTearDown

Time: 0

OK (1 test)