I configured Junit-4.11 on my Mac, compile with javac
has no error, but when I run with java
, I got Could not find class: HelloWorldTest
Here is my HelloWorld.java
and HelloWorldTest.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
and 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.jar
in the current directory with HelloWorld.java
and HelloWorldTest.java
, I also put it in /Library/Java/Extensions
What I tried to solve is to set JAVA_HOME
and CLASSPATH
, but it did not work.
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.
- Delete the
CLASSPATH
in my.zshrc
file(If you are usingBash
I think it is.bashrc
) - Delete the
JUnit-4.11.jar
(or any version you use) in the/Library/Java/Extensions
and any System Directory you put it in. - Try to compile again and run it.
And I set JAVA_HOME
as /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
Thank you.