How to run test cases in a jar file using junit?

W

webzhao

I want to run test cases in a jar file using junit and the jar file is
not in the class path. I wrote the following code, but it does not
work.

import java.net.URL;
import java.net.URLClassLoader;
import junit.framework.TestResult;
import junit.textui.TestRunner;
public class MyTestRunner {
public static void main(String[] args) throws Exception{
URL url = new URL("file:///d:/case.jar");
URLClassLoader loader = new URLClassLoader(new URL[]{url});
loader.loadClass("TestCase1");
TestRunner runner = new TestRunner();
TestResult result = runner.start(new String[]{"TestCase1"});
System.out.println(result.toString());
}
}

any ideas?

Thanks a lot.
 
P

proudbug

I want to run test cases in a jar file using junit and the jar file is
not in the class path. I wrote the following code, but it does not
work.

import java.net.URL;
import java.net.URLClassLoader;
import junit.framework.TestResult;
import junit.textui.TestRunner;
public class MyTestRunner {
public static void main(String[] args) throws Exception{
URL url = new URL("file:///d:/case.jar");
URLClassLoader loader = new URLClassLoader(new URL[]{url});
loader.loadClass("TestCase1");
TestRunner runner = new TestRunner();
TestResult result = runner.start(new String[]{"TestCase1"});
System.out.println(result.toString());
}

}

any ideas?

Thanks a lot.

what's the error message?
 
W

webzhao

Class TestCase1 not found.

I am developing a tool which reads test cases in a jar file and uses
junit to run these cases.

I want to run test cases in a jar file using junit and the jar file is
not in the class path. I wrote the following code, but it does not
work.
import java.net.URL;
import java.net.URLClassLoader;
import junit.framework.TestResult;
import junit.textui.TestRunner;
public class MyTestRunner {
public static void main(String[] args) throws Exception{
URL url = new URL("file:///d:/case.jar");
URLClassLoader loader = new URLClassLoader(new URL[]{url});
loader.loadClass("TestCase1");
TestRunner runner = new TestRunner();
TestResult result = runner.start(new String[]{"TestCase1"});
System.out.println(result.toString());
}

any ideas?
Thanks a lot.

what's the error message?
 
P

proudbug

Class TestCase1 not found.

I am developing a tool which reads test cases in a jar file and uses
junit to run these cases.

The code works on my machine. I think there's something wrong with the
classpath. Double check the url to see if case.jar is where you claim.
Also check if TestCase1 class is in any package. e.g. if TestCase1 is
in package comp.lang.java and under the folder comp/lang/java/ of
case.jar root, then you need to write:
loader.loadClass("comp.lang.java.TestCase1");

I want to run test cases in a jar file using junit and the jar file is
not in the class path. I wrote the following code, but it does not
work.
import java.net.URL;
import java.net.URLClassLoader;
import junit.framework.TestResult;
import junit.textui.TestRunner;
public class MyTestRunner {
public static void main(String[] args) throws Exception{
URL url = new URL("file:///d:/case.jar");
URLClassLoader loader = new URLClassLoader(new URL[]{url});
loader.loadClass("TestCase1");
TestRunner runner = new TestRunner();
TestResult result = runner.start(new String[]{"TestCase1"});
System.out.println(result.toString());
}
}
any ideas?
Thanks a lot.
what's the error message?- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top