How can I export JUnit4 test case as runnable jar file - eclipse

I am trying to export JUnit4 test case as runnable jar file through Eclipse IDE but running into problems.
The test case (exported as JUnit4 test) uses selenium webdriver to perform some GUI task.
I can launch it through Eclipse run configuration (as JUnit test) but when I try to export the same (so that it can be used independently by any third party) to runnable jar, the 'Launch Configuration' drop-down list of 'Runnable JAR File Specification' dialog box is empty.
Is there something missing? How to fix this.

try:
public static void main(String[] args) {
JUnitCore jCore = new JUnitCore();
jCore.run();
}
But then again I don't know where you would read your results and so on.
Means you would need an output file or something.

Related

JUnit5 tests work fine with maven but not when run through Eclipse, "No tests found with test runner 'JUnit 5'."

I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.
I have two test-related files. One is a test suite:
...
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SelectPackages("com.foo.stufftotest")
public class TestSuite {
#BeforeAll
public static void setup() {
...
The other contains the "actual" tests:
package com.foo.stufftotest;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.foo.TestSuite;
import com.foo.business.mechanics.LogicStuff1;
public class BusinessTest {
#Test
public void testLogic1() {
...
}
#Test
public void testLogic2() {
...
}
...
All the testLogicN() methods depend on setup stuff done in TestSuite.setup(). If setup() doesn't run, there are a lot of null values, and it's no surprise that things fail. When I try to run JUnit from the project's context menu, all the tests are triggered and they all fail; the suite doesn't seem to be recognized. When I try to run JUnit specifically from TestSuite.java's context menu, I end up with the error I mentioned at the top of the question.
However, when I run maven test on the project, the suite is triggered properly, and all the tests pass. Therefore, the code itself doesn't seem to be the problem.
I don't remember having this issue with JUnit 4, although I never used JUnit 4 with this particular project.
Am I using Eclipse wrong, or am I using JUnit5 wrong? What's the fix here?
I encountered the same issue in Eclipse Oxygen 4.7.1 despite the fact that my unit tests were properly annotated with org.junit.jupiter.
I used the Maven > Update Project and selected Update project configuration from pom.xml but that did nothing even though I thought it would pick up the fact that I had JUnit5 dependencies in the pom.
My solution:
Open Java Build Path, select Libraries tab and Add Library.
Select JUnit.
Select JUnit5 for JUnit library version.
Once added I was able to manually execute tests from Eclipse. However I'm still not certain why this was not added automatically.
I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.
That's because TestSuite is in fact not a JUnit 5 test class. Since it is annotated with #RunWith (from JUnit 4) it is a JUnit 4 test class. To avoid the pop-up window in Eclipse, simply click on "Run Configurations" and select JUnit 4 instead of JUnit 5 for running the test class.
The other issue you have is that #BeforeAll is an annotation from JUnit Jupiter. Thus, it is simply not supported in a class annotated with #RunWith(JUnitPlatform.class) (unless that class also happens to contain #Test methods for JUnit Jupiter). Thus you will have to find an alternative approach to executing your "set up" code.

Execute/debug .jar file in Netbeans

I have a .jar file and I want to execute/debug it with Netbeans like I can do in IntelliJ. In IntelliJ it's possible to define a run configuration passing the parameters, like executing with java -jar parameters. It's possible to do this with Netbeans?
You can not run arbitrary classes from a jar file in NetBeans. You can only run classes that are part of your (current) project.
You would need to create a Java main class that calls the class you want to debug inside the jar file.
So if the class in the jar file is called some.package.Main you need to create a Java class:
package danyboy.debug;
public class RunIt
{
public static void main(String args[])
{
some.package.Main.main(args);
}
}
Then you can setup a run configuration inside NetBeans to start danyboy.debug.RunIt and pass the necessary parameters.
I assume the jar file is already part of your project's class path.

How to create a groovy jar with eclipse/ggts?

So I create a groovy jar but I'm not able to run it.
I'm using HelloWorld.groovy as an example
class HelloWorld {
static main(args) {
println("Hello World");
}
}
I save it as a jar like I would with any java file via export in eclipse/ggts:
right clicking groovy file in project explorer
click export
select jar File
specify jar file name
specify HelloWorld as the main class in the Manisfest file
keep all the defaults selected
click finish and create the jar
Now I try to run HelloWorld.groovy via java -jar HelloWorld.jar on my command line.
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
When I export it also allows me to export .classpath into my jar.
Shouldn't that include my groovy-all.jar that my GGTS is using?
Am I missing something?
I've been successful with using GroovyWrapper to create groovy jars. I want to see if it's possible with just my IDE.
I think that in eclipse you export an executable jar and that gives the option to package the dependencies?

ClassNotFoundException for a test class in Eclipse maven project

I create the simplest maven project in eclipse. I add one JUnit test in src/test/java. Then I create a simple application (in src/main/java) that tries to do Class.forName("package.MyTestClass");. The class is not found even though the eclipse project is defined to export the src/test/java as a source folder.
What is going on? How can I fix this?
I wanted to do the following with TestNG
public static void main(String[] args) throws ClassNotFoundException {
// Class.forName("package.MyTestClass");
TestNG.main(new String[]{"testng.xml"});
}
It fails because it cannot find the test classes with:
[TestNG] [ERROR] Cannot find class in classpath: package.MyTestClass
If I have understood you correctly, your project is like this
/src/main/java/Main.java (or whatever you have called it)
/src/test/java/package/MyTest.java
In your Main class you are trying to create an instance of a test class. This will not work. The classes in your main directory are available to the classes in your test directory, not the other way around, which makes sense. Your test classes must know about your application to test it, but your application should not know about the test classes

Classpath problems when running JUnit tests from Eclipse with Gradle build

I have a Gradle project that declares a test-only dependency on an XML data file, and then loads the file from the classpath. When I run the tests directly in Gradle from the command line, everything works fine, but when I run "gradlew eclipse", refresh the project in Eclipse, and then try running the test from Eclipse (Debug As -> JUnit Test), the test fails because it's unable to find the XML file and the classpath (as accessed from the Properties context menu item on the process in the Debug view) shows no indication of the XML file being included on the classpath.
The behavior I'm seeing has some commonality with http://gradle.1045684.n5.nabble.com/gradle-junit-tests-resources-and-classpath-td4418753.html#a4420758, but Sean's problem was the reverse: his tests ran properly under Ant (but he never mentioned trying to run directly from the Eclipse JUnit plugin), but not under Gradle.
Here's the relevant part of build.gradle:
dependencies {
testCompile group: 'com.mycompany', name: 'MyConfigFile', version: '0.0.0+dirty', ext: 'xml' }
Because the only resources that URLClasspathLoader can load directly from the file system are JARs, I'm using the following static method to search the classpath for files that match the filename I need to load:
public static String getFullPathForResourceDirectlyOnClasspath(String nameFragment) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
for (URL url: ((URLClassLoader)cl).getURLs()){
String fullPath = url.getFile();
if (fullPath.contains(nameFragment)) {
return fullPath;
}
}
return null;
}
I call that method as follows:
getFullPathForResourceDirectlyOnClasspath("/MyConfigFile-");
When I run that code from Gradle ("gradlew build"), it finds the file and my test succeeds. When I run it from Eclipse (Debug As -> JUnit Test), it fails to find that file on the classpath (because the Eclipse JUnit plugin doesn't put it there) and that call returns null.
I've tried changing the configuation from testCompile to compile to see if that made a difference, but it doesn't change anything (and perhaps tellingly, my .classpath doesn't have any entry for the XML file even when the compile configuration is selected).
Does anyone know of a way to make this work? Am I just missing something that should be obvious?
It seems that you are abusing the class path to pass a single argument (the absolute file path of the XML file) to a test. Instead, you should either put the XML file on the (test) class path in the correct way (it needs to go into a directory or Jar file that's listed on the class path) and load it correctly (e.g. with getClass().getClassLoader().getResource("some/resource.xml")), or pass the file path to the test as a system property. Naturally, the latter will be harder to make work for different environments (say Gradle build and IDEs).