0

I want to run a simple JUnit test with Jenkins, and first I want to try it in command prompt, if it works.

The JUnit.jar file is in C:\junit.jar

I have a dynamic web project, in which I have a "test" package in which I have a LoginTest class

    package test;
    import junit.framework.TestCase;
    public class LoginTest extends TestCase { 
        @Test
        public void testLogin() {
            ....
            assertstuff...
        }
    }

I have a separate project with a TestSuite class :

    package test;
    import org.junit.extensions.cpsuite.ClasspathSuite;
    import org.junit.runner.RunWith;
    @RunWith(ClasspathSuite.class)
    public class TestSuite {

    }

Then I added my LoginTest class to the TestSuite build path.

If I run the TestSuite with JUnit within Eclipse it works, but I want to run it from cmd.

I tried the following in cmd :

    java -cp C:\junit.jar;D:\Documents\eclipse\blablabla\project_path\bin test.TestSuite

but it's not working. Could you help me with this issue?

Laureant
  • 979
  • 3
  • 18
  • 47
  • possible duplicate of [How to run JUnit test cases from the command line](http://stackoverflow.com/questions/2235276/how-to-run-junit-test-cases-from-the-command-line) – Jens Jun 02 '14 at 18:36
  • well, i didn't learn anything from that, that's why i made this topic – Laureant Jun 02 '14 at 19:00
  • Then try [this tutorial](http://www.tutorialspoint.com/junit/junit_executing_tests.htm) – Jens Jun 02 '14 at 19:02

1 Answers1

2

Read your command line carefully:

java -cp C:\junit.jar;... test.TestSuite
#        (classpath)      (main class)

You're telling Java to run your TestSuite's main method, which doesn't exist. Now compare the one from the SO question that Jens linked:

java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
#        (classpath)               (main class - in JUnit!)   (first argument)

Rather than trying to run TestSuite, the second command asks java to run JUnitCore, which has a main method java can call. After that class name, pass your test suite as the first parameter, which tells JUnitCore to load and run that class in particular.

Try this:

java -cp C:\junit.jar;D:\Documents\eclipse\blablabla\project_path\bin org.junit.runner.JUnitCore test.TestSuite
#        (your full classpath)                                        (main class)               (your test class)
Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • This is what I get : Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) – Laureant Jun 03 '14 at 13:17
  • As posted on the [JUnit Download and Install Guide](https://github.com/junit-team/junit/wiki/Download-and-Install), you need both junit.jar and hamcrest-core.jar on your classpath. Otherwise, Hamcrest classes (like org.hamcrest.SelfDescribing) will not be found (NoClassDefFoundError). – Jeff Bowman Jun 03 '14 at 13:22