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

Discussion in 'Java' started by webzhao, Dec 13, 2007.

  1. webzhao

    webzhao Guest

    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.
    webzhao, Dec 13, 2007
    #1
    1. Advertising

  2. webzhao

    proudbug Guest

    On Dec 13, 5:05 am, webzhao <> wrote:
    > 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?
    proudbug, Dec 13, 2007
    #2
    1. Advertising

  3. webzhao

    webzhao Guest

    Class TestCase1 not found.

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

    On Dec 13, 11:43 pm, proudbug <> wrote:
    > On Dec 13, 5:05 am, webzhao <> wrote:
    >
    >
    >
    > > 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?
    webzhao, Dec 14, 2007
    #3
  4. webzhao

    proudbug Guest

    On Dec 14, 3:57 am, webzhao <> wrote:
    > 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");


    > On Dec 13, 11:43 pm, proudbug <> wrote:
    >
    >
    >
    > > On Dec 13, 5:05 am, webzhao <> wrote:

    >
    > > > 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 -
    proudbug, Dec 14, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    2
    Views:
    3,868
    news.rcn.com
    Jul 16, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    555
    Arnold Peters
    Jan 5, 2005
  3. Phlip
    Replies:
    0
    Views:
    825
    Phlip
    Sep 22, 2005
  4. Phlip
    Replies:
    0
    Views:
    654
    Phlip
    Sep 22, 2005
  5. David Mitchell
    Replies:
    10
    Views:
    245
Loading...

Share This Page