Run JUnit from makefile (java.lang.NoClassDefFoundError) - junit

I've got a JUnit project that I want to add in a Toolchain. This toolchain using Makefile.
The Makefile is not in my prject directory.
So I tryied to put a "test" rules in this makefile as following :
PROJPATH=TestJUnit/Test/
PACKAGE=com/project/package/name/
LIB=$(PROJPATH)lib/
SRC=$(PROJPATH)src/$(PACKAGE)
BIN=$(PROJPATH)bin/$(PACKAGE)
--------- All the acces to lib.jar and .class used during the test ------
CLASSPATH=.:$(LIB)junit.jar:$(LIB)opal-library.jar:$(SRC):$(BIN)
--------- Name of test in order of wanted excecution -------------
JAVASRC= Test1\
Test2\
Test3\
test:
java -classpath $(JUNITPATH) org.junit.runner.JUnitCore $(JAVASRC)
When I try to launch 'make test'
I've got the following message:
JUnit version 4.11
Exception in thread "main" java.lang.NoClassDefFoundError: Test1 (wrong name: com/project/name/Test1)
I try everything possible even to launch the java command in my project directory but nothing work.
All the .class and .java are respectivly in the bin and src directorys.

ok Guys I fond the problem.
It's seems that the class path was not bad but the way of calling the test was wrong.
By simply add the /src and /bin in the classpath (without the sub package directory) and concat the package name to the test class name the test are succesfully launched.
something like that:
PROJPATH=TestJUnit/Test/
PACKAGE=com.project.package.name.
LIB=$(PROJPATH)lib/
SRC=$(PROJPATH)src/
BIN=$(PROJPATH)bin/
--------- All the acces to lib.jar and .class used during the test ------
CLASSPATH=.:$(LIB)junit.jar:$(LIB)opal-library.jar:$(SRC):$(BIN)
--------- Name of test in order of wanted excecution -------------
JAVASRC= $(PACKAGE)Test1\
$(PACKAGE)Test2\
$(PACKAGE)Test3\
test:
java -classpath $(JUNITPATH) org.junit.runner.JUnitCore $(JAVASRC)

Related

Caused by: com.typesafe.config.ConfigException$Missing

when i run my apps in Idea it is good. But when i package and run with java -jar in linux. I got this exception.
exception
the problem code is
val config = ConfigFactory.parseFile(new File("src/main/resources/master.conf"))
master = context.actorSelection(s"akka.tcp://masterSystem#${config.getString("akka.remote.netty.tcp.hostname")}:${config.getString("akka.remote.netty.tcp.port")}/user/Master")
the structure of my module
src/main/resources path doesn't exist in your jar and that's by design.
Try first getting your config in your code like this:
val config = ConfigFactor.load()
and then pass java -Dconfig.location=master.conf jar ... to your program

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

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

Running JUnit from command prompt: classpath confusion

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