user6405863 user6405863 - 6 months ago 33
Java Question

Maven running external JUnit Jar causes java.lang.Exception: No runnable methods

I have project where a user creates and uploads a

jar
filled with
JUnit
tests. My project is running in
Maven
and for some reason it will not run. I can hard code some
JUnit
tests and they work fine but once I put the exact code in a .jar file the file won't run. This is my first question on Stack Overflow and I would appreciate any help.

Here is my code

simpletest.jar

import org.junit.Test;
public class simpletest {
@Test
public void sayHello() {
System.out.println("Simple test ran");
}}


runjar.java

URL[] classLoaderUrls = new URL[]{new URL("file:///c:/Work/simpletest.jar")};
URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
Class<?> appiumClass = null;
try {
appiumClass = urlClassLoader.loadClass("simpletest.simpletest");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Run the class using JUnitCore
JUnitCore junit = new JUnitCore();
Result result = junit.run(appiumClass);
//Display if test was successful
System.out.println(result.wasSuccessful());

ALM ALM
Answer

When you are loading the URLClassLoader you need to supply the current ClassLoader for it to load the required JUnit libraries.

        //Code to run junit test
        URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls, getClass().getClassLoader());
Comments