Anjana Ouseph
Anjana Ouseph

Reputation: 31

Getting Started with Junit. Error while compiling through command line

I have downloaded junit-4.10.jar and hamcrest-2.2.jar to a folder C:/JUnit.

In Environment Variables->System Variables I Have set JUNIT_HOME as C:\JUnit and CLASSPATH as %CLASSPATH%;%JUNIT_HOME%\junit-4.10.jar;.;

Then I created a folder C:/JUNIT_WORKSPACE and added two files TestJunit.java and TestRunner.java. Now in command line terminal when I enter: C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java

It throws the below error:

   C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
TestJunit.java:1: error: package org.junit does not exist
import org.junit.Test;
                ^
TestJunit.java:2: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
                       ^
TestJunit.java:2: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
TestRunner.java:1: error: package org.junit.runner does not exist
import org.junit.runner.JUnitCore;
                       ^
TestRunner.java:2: error: package org.junit.runner does not exist
import org.junit.runner.Result;
                       ^
TestRunner.java:3: error: package org.junit.runner.notification does not exist
import org.junit.runner.notification.Failure;
                                    ^
TestJunit.java:5: error: cannot find symbol
   @Test
    ^
  symbol:   class Test
  location: class TestJunit
TestJunit.java:9: error: cannot find symbol
      assertEquals("Junit is working fine",str);
      ^
  symbol:   method assertEquals(String,String)
  location: class TestJunit
TestRunner.java:7: error: cannot find symbol
      Result result = JUnitCore.runClasses(TestJunit.class);
      ^
  symbol:   class Result
  location: class TestRunner
TestRunner.java:7: error: cannot find symbol
      Result result = JUnitCore.runClasses(TestJunit.class);
                      ^
  symbol:   variable JUnitCore
  location: class TestRunner
TestRunner.java:9: error: cannot find symbol
      for (Failure failure : result.getFailures()) {
           ^
  symbol:   class Failure
  location: class TestRunner
11 errors

What did I do wrong here? I assume I have set the HOME and Classpath correctly.

Upvotes: 1

Views: 175

Answers (1)

Anjana Ouseph
Anjana Ouseph

Reputation: 31

The issue is resolved. I changed the CLASSPATH to %CLASSPATH%;.;%JUNIT_HOME%\junit-4.10.jar;%JUNIT_HOME%\hamcrest-core-2.2.jar;

and after that on running the commands in cmd, I get the correct response:

C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java

C:\JUNIT_WORKSPACE>java TestRunner
true

C:\JUNIT_WORKSPACE>

Upvotes: 1

Related Questions