Running JUnit from command prompt: classpath confusion - junit

I'm having trouble getting from a working JUnit command line invocation to something only slightly more complicated. At the outset, 'hw' is a class with no package specified. The following command is successfully executed from the dir in which hw.class lives:
java -cp /usr/share/java/hamcrest-core.jar:/usr/lib/eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar:./ org.junit.runner.JUnitCore hw
I now specify a package 'p' for hw, move hw.java into subdir 'p', and recompile. How can I modify the above command to have the class successfully tested? I've have thought that
java -cp /usr/share/java/hamcrest-core.jar:/usr/lib/eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar:p org.junit.runner.JUnitCore p/hw
would work from the 'superdir' of p, but it doesn't...

There are two problems with your command:
The classpath must include the directory that is at the root of the package hierarchy, i.e. not p but the directory containing p.
The argument to JUnitCore is the fully qualified name of the class, i.e. p.hw not p/hw

The classes are missed at the time of execution of test in classpath of the project
s
Solution : add in your pom
commons-discovery
commons-discovery
0.5
test

Related

How to setup Robot Framework standalone jar with SwingLibrary?

I'm using Robot Framework with SwingLibrary to test a Java Swing based application. Since I'm not used to Python and also don't want to setup the Python environment, I decided to go with the Robot standalone JAR version (current version 2.8.4).
My problem is the setup in combination with SwingLibrary (version 1.8.0). I don't know where to put the library such that it gets recognized by Robot.
So far, I have the following test case (mytest.txt):
*** Settings ***
Library SwingLibrary
*** Test Cases ***
MyTestCase
Start Application MyApp
I tried with putting the standalone jar in conjunction with the test case in a folder, and created one subfolder (called it Lib) where I put the SwingLibrary JAR (and later also extracted the JAR).
I added the SwingLibrary as well as my own application to the classpath, tried executing robot the following way:
java -Xbootclasspath/a:Lib/swinglibrary-1.8.0.jar:Lib/MyApp.jar -jar robotframework-2.8.4.jar mytest.txt
and also with
java -jar robotframework-2.8.4.jar mytest.txt
I always get one of the following errors:
[ WARN ] Imported library 'SwingLibrary' contains no keywords
==============================================================================
Mytest
==============================================================================
MyTestCase | FAIL |
No keyword with name 'Start Application' found.
or
[ ERROR ] Error in file 'mytest.txt': Importing test library 'SwingLibrary' failed: ImportError: No module named SwingLibrary
You can use the standalone jar without the -jar option, allowing you to specify the classpath in the standard manner. The main class for the standalone jar is org.robotframework.RobotFramework, so the syntax would be
java -cp robotframework-2.8.4.jar:Lib/swinglibrary-1.8.0.jar:Lib/MyApp.jar org.robotframework.RobotFramework
Slightly more verbose but it's standard and so avoids any oddnesses caused by using the non-standard -Xbootclasspath option.

Unable to run junit test from command prompt

I'm trying to run a JUnit test case from command lineThe 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:
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:
put the junit.jar library in your classpath and;
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.
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

I give the absolute path but java cannot a class

I cannot understand why this
ioannis#ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.jar org.junit.runner.JUnitCore /opt/CalculatorTest
gives the error:
Could not find class: /opt/CalculatorTest
and this work fine:
ioannis#ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.jar:/opt org.junit.runner.JUnitCore CalculatorTest
JUnit version 4.6
.
Time: 0.005
OK (1 test)
in the first I give the full path for class /opt/CalculatorTest
The classname is intended to be just the class name, not a path. If it is in a package, you can have foo.Bar. If it is in the default package, you can only have Bar.
As you noticed, the classpath is the place to identify the physical directories Java should look in to find the class.

Error while running JUnit test from command line

I am trying to run a single JUnit test from the command line but I am getting an error.
I could compile the JUnit test successfully and the class file gets created in the correct location.
But when I try to run it using:
C:\Program Files\Java\jdk1.7.0_01\bin>java org.junit.runner.JUnitCore C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class
I get the error:
JUnit version 4.8.1
Could not find class: C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDesc
ribbing
I don’t know why it is not able to find the class even though it exists in the said location.
You need to specify the name of the class on the command line, not the filename:
java org.junit.runner.JUnitCore com.org.tests.Nav
From the javadoc for JUnitCore:
JUnitCore is a facade for running tests. It supports running JUnit 4
tests, JUnit 3.8.x tests, and mixtures. To run tests from the command
line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 ....
For one-shot test runs, use the static method runClasses(Class[]). If
you want to add special listeners, create an instance of
org.junit.runner.JUnitCore first and use it to run the tests.
and you will have to add the bin directory (note NOT the src) to the classpath of the command line as well. This may look like:
java -cp C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\bin org.junit.runner.JUnitCore com.org.tests.Nav

How to register a JDBC driver using jruby-complete.jar?

I'm trying to write a script that is executed with the jruby-complete.jar like so:
java -cp derby.jar; -Djdbc.drivers=org.apache.derby.jdbc.EmbeddedDriver -jar jruby-complete.jar -S my_script.rb
I'm using JVM 1.6.0_11 and JRuby 1.4.
In my jruby script I attempt to connect to the database like this.
connection = Java::com.sql.DriverManager.getConnection("jdbc:derby:path_to_my_DB")
This throws a java.sql.SQLException: "No suitable driver found" exception.
I've tried manually loading the driver into the class loader using Class.forName which gives me the same error.
It looks like to me that the class loader being used by the DriverManager is not the same as the current thread's. I've tried setting the current thread's class loader using:
JThread = java.lang.Thread
...
class_loader = JavaLang::URLClassLoader.new(
[JavaLang::URL.new("jar:file:/derby.jar!/")].to_java(
JavaLang::URL),JRuby.runtime.jruby_class_loader)
JThread.currentThread().setContextClassLoader(class_loader )
But this doesn't help.
Any ideas?
OK I downloaded jruby-complete.jar and had a go....
This seems to work for me:
java -classpath c:\ruby\db-derby-10.5.3.0-bin\lib\derby.jar;jruby-complete-1.4.0.jar org.jruby.Main -S derby.rb
When using the -jar switch, the -classpath option is ignored (maybe the CLASSPATH shell var is too). But on the above line, we put both required jars on the class path and pass the class name to execute (i.e. org.jruby.Main). The script being passed in is as per my other answer.
Another option (which I have not tried) would be to alter the jruby-complete.jar manifest file to specify as classpath, as described here:
Adding Classes to the JAR File's Classpath
First, make sure your driver jar is not corrupted (this made me waste a couple of days one time).
Second, read this about JRuby/Java classloade: JRuby Wiki
Third (because I haven't played with "jruby-complete") try this simple script and then see if you can adapt as you need.
require 'java'
require 'C:\ruby\db-derby-10.5.3.0-bin\lib\derby.jar' # adjust for your machine
include_class "java.sql.DriverManager"
derby = org.apache.derby.jdbc.EmbeddedDriver.new
connection = DriverManager.getConnection("jdbc:derby:derbyDB;create=true")