Could not find class in Junit-4.11

I configured Junit-4.11 on my Mac, compiling with javac has no error, but when I start with java , I got Could not find class: HelloWorldTest

Here are 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'm running with

javac -classpath ./ HelloWorldTest.java

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

I got

 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 was trying to solve was to set JAVA_HOME and CLASSPATH , but that didn't work.

Can someone point out what is going wrong? I was very confused.

Thankyou.

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

  • Delete CLASSPATH in my .zshrc file (if you use Bash , I think it is .bashrc )
  • Remove junit-4.11.jar (or any version you use) in /Library/Java/Extensions and any system directory in which you entered it.
  • Try compiling and running it again.

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

Thanks.

+4
source share
2 answers

Update . The original solution for posters is given in the comment below. This is due to the absence of junit jar files in /Library/Java/Extensions and not having 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.


In temp dir (java files from your question):

 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

Output:

 HelloWorldTest JUnit version 4.11 @BeforeClass - oneTimeSetUp .@Before - setUp @Test - testOutput @After - tearDown @AfterClass - oneTimeTearDown Time: 0,004 OK (1 test) 

I would suggest repeating from scratch

+9
source

Ok, I tested the same thing. Provide steps for this:

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

The first HelloWorld.java process:

 javac HelloWorld.java 

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

The following HelloWorldTest.java process:

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

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

 java -classpath C:\Himanshu_Work\repo\junit\junit\4.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) 
0
source

Source: https://habr.com/ru/post/1489030/


All Articles