2

I am trying to use junit in the terminal. I have a class Abc.java

class Abc{
    public int add(int a, int b){
         return a+b
    }
}

and i have created a class AbcTest.java

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

class AbcTest {
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        Abc instance = new Abc();
        int expResult = 0;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
    }
}

when i am running the command

javac -cp /usr/share/java/junit4.jar AbcTest.java

I am getting the following error output

AbcTest.java:16: error: cannot find symbol
Abc instance = new Abc();
^
symbol:   class Abc
location: class AbcTest
AbcTest.java:16: error: cannot find symbol
Abc instance = new Abc();
                   ^
symbol:   class Abc
location: class AbcTest
2 errors

I tried to build the project with the command

javac -cp /usr/share/java/junit4.jar *.java

it build up properly, but running this command

java -cp /usr/share/java/junit4.jar org.junit.runner.JUnitCore AbcTest

throws the following error

JUnit version 4.11
Could not find class: AbcTest
Time: 0.002
OK (0 tests)
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
  • Take a look at this thread: http://stackoverflow.com/questions/2235276/how-to-run-junit-test-cases-from-the-command-line There are so many suggestions and discussions about running from terminal – ganeshvjy Jul 19 '15 at 15:56

2 Answers2

7

You need to add current directory (or the directory where the compile class file are) along with the necessary jar files to class path.

java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore AbcTest

FYI When I tried to run the same test case I used (jar files and class files in current directory)

javac -cp junit-4.11.jar:. AbcTest.java
java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore AbcTest

And had to modify the AbcTest.java as

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;


public class AbcTest {
    @Test
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        Abc instance = new Abc();
        int expResult = 0;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
    }
}

Changes:

  1. public class AbcTest
  2. @Test annotation for testAdd method
shanmuga
  • 4,329
  • 2
  • 21
  • 35
  • Thanks a lot, i was missing out on adding the current directory while running. – isnvi23h4 Jul 20 '15 at 05:22
  • however the problem gets back when I move the Abc.java to src/Abc.java and AbcTest.java to test/AbcTest.java. – isnvi23h4 Jul 20 '15 at 06:16
  • You need to add the directory with the compiled class files to classpath using -cp option. In the above example I used '.' It denotes current directory. – shanmuga Jul 21 '15 at 06:11
0

JUnit's own Get Started page is a detailed How-To for running JUnit from the command-line.

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72