0

I want to launch my JUnit test using the command line on windows. I have already seen thread about it in this page : How to run JUnit test cases from the command line

This is the command line i use to launch it :

java -cp C:\Junit\junit-4.11.jar org.junit.runner.JUnitCore
    'C:\Users\FORNAME NAME\workspace\Project\test\TheClassIWantToTest.class'

And i have the following error :

Could not find class : TheClassIWantToTest Exception etc

I'm sure that I did not make an error writing the way to the file because I used auto completion. And the class is found when I launch it without JUnit. I have tried with and without the .class at the end of the filename.

I have the exact same error when i try to launch a TestNG by command line.

Anyone already had this problem ?

Community
  • 1
  • 1
Omegaspard
  • 1,828
  • 2
  • 24
  • 52

2 Answers2

0

How about:

java -cp .;C:\Junit\junit-4.11.jar org.junit.runner.JUnitCore
    'C:\Users\FORNAME NAME\workspace\Project\test\TheClassIWantToTest.java'

Or try to create a Test Runner Class.

See this link for an example on how to do that.

Draken
  • 3,134
  • 13
  • 34
  • 54
Carlos Andrés García
  • 1,483
  • 2
  • 15
  • 30
  • For anyone wondering what's different between the two java commands: one has `-cp .;C:\bla`, the other one just `-cp C:\bla` (missing the current working directory, indicated by the `.`) – stuXnet May 17 '16 at 17:52
  • @stuXnet the dot (`.`) represents the existing classpath, or as well call it the current dicrectory. What it means, if you are in the path `C:\Users\user\workspace` and execute the command with dot(.) the java or javac command will looking for resources(whatever it was, a `.class` , `.jar` or `.java`) in that path. – Carlos Andrés García May 17 '16 at 18:09
  • I have the same result, i will try the Test Runner class, but can someone tell me why it's not working ? – Omegaspard May 18 '16 at 08:11
  • @CarlosAndrésGarcía I know, but thanks for adding that but of information for others :) I was just comparing your snippet with that from the OP, and thought "Huh, aren't they the same?", until I noticed the dot :) – stuXnet May 19 '16 at 08:25
0

This time I've followed Junit Getting Started link, and the example works.

Below are the steps that I did in Windows;

1 : I created the folder test on my Desktop

2 : Inside test , I create the Calculator(Calculator.java) file

public class Calculator {
  public int evaluate(String expression) {
    int sum = 0;
    for (String summand: expression.split("\\+"))
      sum += Integer.valueOf(summand);
    return sum;
  }
}

3. Inside test , I created the CalculatorTest(CalculatorTest.java) file

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

4: I downloaded Junit 12 jar from Maven Repository(Note : this example do not use maven, I use only maven repository to get the jar file).

5: I downloaded hamcrest-core 1.3 jar from Maven repository (Note : this example do not use maven, I use only maven repository to get the jar file)

6: The test folder looks like this enter image description here

7: I opend the CMD and go to root of test folder and execute this command

javac -cp junit-4.12.jar;hamcrest-core-1.3.jar *.java

Note: *.java compiles all the files with java extension, in this case CalculatorTest.java and Calculator.java

8: The test folder looks like now like this enter image description here

9: I executed the command

java -cp junit-4.12.jar;hamcrest-core-1.3.jar;. org.junit.runner.JUnitCore CalculatorTest

Note: this sentence must to add the dot(.) in the classpath, otherwise Calculator file is not find it and you will get

initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class

And finally I get the unit test running by command line how you can see below

enter image description here

At this point the example does not inlcude the package keyword, which has to add the physical folders on Windows to running the test, lets explain this case

1: Let's add the package "stackoverflow.junit.test" to files Calculator.java and CalculatorTest.java

package stackoverflow.junit.test;

public class Calculator {
  public int evaluate(String expression) {
    int sum = 0;
    for (String summand: expression.split("\\+"))
      sum += Integer.valueOf(summand);
    return sum;
  }
}

and

package stackoverflow.junit.test;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

    public class CalculatorTest {
      @Test
      public void evaluatesExpression() {
        Calculator calculator = new Calculator();
        int sum = calculator.evaluate("1+2+3");
        assertEquals(6, sum);
      }
    }

2) now it is posible to compile the code with the same instruction show before javac -cp junit-4.12.jar;hamcrest-core-1.3.jar *.java . However if I want to run the test I will get java.lang.NotClassDefFoundError

enter image description here

To solve this problem the folders had to been created physically and added the Calculator.java and CalculatorTest.java

enter image description here

  1. With my right structure of folder I compiled and run the test, but having in account that this command are executed from test folder root on. What is more, the name of the package has to be add.

    javac -cp junit-4.12.jar;hamcrest-core-1.3.jar stackoverflow/junit/test/*.java

    java -cp junit-4.12.jar;hamcrest-core-1.3.jar;. org.junit.runner.JUnitCore stackoverflow.junit.test.CalculatorTest

enter image description here

Recommendation: All this will be less tedious if you use Maven or Gradle.

Recommendation: Check the structure of your proyect as well your CLASSPATH.

Carlos Andrés García
  • 1,483
  • 2
  • 15
  • 30