junit: could not find test class

2019-07-01 22:43发布

问题:

I've looked at the duplicate questions here and elsewhere, including the JUnit FAQ (http://junit.sourceforge.net/doc/faq/faq.htm#running_1).

junit-4.10.jar is at /Library/Java/Extensions which is automatically on the classpath.

My test class is compiled in my working directory. It is not part of any package. However, none of these work:

java org.junit.runner.JUnitCore TestBoard
java -cp . org.junit.runner.JUnitCore TestBoard

Both return:

JUnit version 4.10
Could not find class: TestBoard

Time: 0.001

OK (0 tests)

回答1:

The best way to determine if this is a problem with JUnit or a problem with your class and/or classpath would be to add a main method to your test class:

public class TestBoard extends TestCase {

  public void testThatShouldFail() {
    fail();
  }

  public static void main(String[] args) {
    System.err.println("Hello World!");
  }
}

Then you can run:

javac TestBoard.java
java TestBoard

If this does not print "Hello World!" then it's some problem with your environment (classpath issue, etc) or your test class.

My best guess is you forgot to make the class public.



回答2:

Recent I had similar problem with my junits tests. Running tests ('mvn test' in my case) caused few types of misleading execptions. As it turned out i was using jdk 1.6 in my project. When i've switched to jdk 1.7 everything ended.



标签: java junit