Riy1234
Riy1234

Reputation: 39

Unable to run junit test from command prompt

I'm trying to run a JUnit test case from command line
The code I followed is set to bin dir

c:/eclipse/workspace/sample/bin> java -cp C:\Ram Doc\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705.junit.jar C:\Ram Doc\eclipse\workspace\Script_Bvt\bin org.junit.runner.JUnitCore login_sanity(That's my class Name)

Error message is

C:\Ram Doc\eclipse\workspace\Script_Bvt\bin>java -cp C:\Ram Doc\eclipse\plugins\
org.junit_4.8.2.v4_8_2_v20110321-1705.junit.jar java org.junit.runner.JUnitCore
login_sanity
Exception in thread "main" java.lang.NoClassDefFoundError: Doc\eclipse\plugins\o
rg/junit_4/8/2/v4_8_2_v20110321-1705/junit/jar
Caused by: java.lang.ClassNotFoundException: Doc\eclipse\plugins\org.junit_4.8.2
.v4_8_2_v20110321-1705.junit.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Doc\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110
321-1705.junit.jar.  Program will exit.

If i keep the login_sanity at other location and execute:

C:\Selenium>javac -cp "C:\Selenium\junit4.10\junit4.10\junit-4.10.jar;." org.junit.runner.JUnitCore login_sanity

I get the following error:

Class names, 'org.junit.runner.JUnitCore,login_sanity', are only accepted if 
annotation processing is explicitly requested

The following shows my complete steps: enter image description here

Upvotes: 0

Views: 5244

Answers (2)

Abdullah Altamimi
Abdullah Altamimi

Reputation: 1

Add the junit jar to the classpath
Add the tested classes to the classpath
Run junit.textui.TestRunner testclass

java -classpath .;c:\path\name.jar;c:\path\to\bin org.junit.runner.JUnitCore nameofclass

Upvotes: 0

pestrella
pestrella

Reputation: 9936

In general running a Java class via command line is done like this (on Windows, which it appears you are using):

java -cp "jar1;jar2;dir\*" my.app.package.MainClass my_arguments

From what I can tell, you are trying to execute your test class, login_sanity, via JUnitCore. In other words you're executing the JUnitCore class with your test class as the argument; and you require the junit.jar library in order to run the JUnitCore class.

In order run this command, you would need to:

  1. put the junit.jar library in your classpath and;
  2. specify the main class you want to execute, JUnitCore, along with the arguments you want to pass to the main class, i.e. login_sanity

So it's like this:

java -cp "C:\path\to\junit.jar;C:\path\to\bin\*" org.junit.runner.JUnitCore login_sanity

The above command assumes your class login_sanity is in the default package, i.e. no package, and in the bin directory.

If your class is not in the default package, i.e. you have declared a package inside your login_sanity class, then you would need to use its fully qualified name in the command line. Here's an example --

Say your class is in the following package: my.app.login. In other words the first few lines of your java class is:

package my.app.services;

public class login_sanity {
  /* your tests go here */
}

In this case you would execute JUnitCore like so:

java -cp "C:\path\to\junit.jar;C:\path\to\bin\*" org.junit.runner.JUnitCore my.app.services.login_sanity

As an aside, typical Java convention is to name your classes in camel case, i.e. LoginSanity.

Upvotes: 1

Related Questions