0

I know this is very common problem and i already did search but somehow it could not resolve my problem.

Here is the problem context:

Created a maven project(Using eclipse with maven quickstart and used Junit version 4.12) for my Junit test cases. Following is the structure- enter image description here

And this is the content of Junit file-

package JunitRnD.JunitArtifact;

import static org.junit.Assert.*;

import java.util.Stack;

import org.junit.Test;

public class AppTest {

    @Test
    public void emptyTest() {
        Stack<String> stack = new Stack<String>();
        assertTrue(stack.isEmpty());
    }
}

Project is located under this location:

/Users/_eclipseWork/JunitArtifact

And this is the content of the directory- enter image description here

Junit test works from eclipse. But i want to run it from command line. So I used following command-

cd /Users/_eclipseWork/JunitArtifact

java -cp .:/Users/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar org.junit.runner.JUnitCore JunitRnD/JunitArtifact/AppTest

I also used other variant per directory structure, but always received following error-

Caused by: java.lang.ClassNotFoundException: JunitRnD/JunitArtifact/AppTest

What am i missing here?

PS: Also tried with putting .java and .class extension.

Sam
  • 859
  • 3
  • 12
  • 23
  • maven has a "command" to run single tests, FWIW http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html – rogerdpack Aug 25 '16 at 19:14
  • I am working on a use case where need to run test case by a process without Maven – Sam Aug 25 '16 at 19:14
  • http://stackoverflow.com/a/3997971/2464657 – Adam Aug 25 '16 at 19:19
  • @Adam Rosini Thanks for the link. But i already tried and it went in vain. Can you please see my directory structure and let me know from where should i run the command? – Sam Aug 25 '16 at 20:36

1 Answers1

2

You need to run Maven to run your test.

mvn -Dtest=JunitRnD.JunitArtifact.AppTest test

Or, iF you want to run without Maven you need a Test Runner.

Take a look at:

https://github.com/junit-team/junit4/wiki/test-runners

You can do:

java org.junit.runner.JUnitCore TestClass1 [...other test classes...]
Joe Kampf
  • 339
  • 2
  • 8
  • I like the idea of mvn with -Dtest (+1). Thank you for sharing this. But it still does not solve my problem. As i want to run the test cases from command line and also aware of Junitcore and applied the same but its failing as mentioned in my question. – Sam Aug 25 '16 at 20:34