How do I programmatically run all the JUnit tests in my Java application?

后端 未结 6 1936
逝去的感伤
逝去的感伤 2021-02-02 18:13

From Eclipse I can easily run all the JUnit tests in my application.

I would like to be able to run the tests on target systems from the application jar, without Eclipse

6条回答
  •  孤街浪徒
    2021-02-02 18:45

    Based on http://burtbeckwith.com/blog/?p=52 I came up with the following. It seems to work well.

    I can run it from within my code with:

    org.junit.runner.JUnitCore.main("suneido.AllTestsSuite");
    

    One weak point is that it relies on a naming convention ("Test" suffix) to identify tests. Another weak point is that the name of the jar file is hard coded.

    package suneido;
    
    import java.io.IOException;
    import java.lang.reflect.Modifier;
    import java.util.*;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    import org.junit.runners.model.InitializationError;
    
    /**
     * Discovers all JUnit tests in a jar file and runs them in a suite.
     */
    @RunWith(AllTestsSuite.AllTestsRunner.class)
    public final class AllTestsSuite {
        private final static String JARFILE = "jsuneido.jar";
    
        private AllTestsSuite() {
        }
    
        public static class AllTestsRunner extends Suite {
    
            public AllTestsRunner(final Class clazz) throws InitializationError {
                super(clazz, findClasses());
            }
    
            private static Class[] findClasses() {
                List classFiles = new ArrayList();
                findClasses(classFiles);
                List> classes = convertToClasses(classFiles);
                return classes.toArray(new Class[classes.size()]);
            }
    
            private static void findClasses(final List classFiles) {
                JarFile jf;
                try {
                    jf = new JarFile(JARFILE);
                    for (Enumeration e = jf.entries(); e.hasMoreElements();) {
                        String name = e.nextElement().getName();
                        if (name.startsWith("suneido/") && name.endsWith("Test.class")
                                && !name.contains("$"))
                            classFiles.add(name.replaceAll("/", ".")
                                    .substring(0, name.length() - 6));
                    }
                    jf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
    
            private static List> convertToClasses(
                    final List classFiles) {
                List> classes = new ArrayList>();
                for (String name : classFiles) {
                    Class c;
                    try {
                        c = Class.forName(name);
                    }
                    catch (ClassNotFoundException e) {
                        throw new AssertionError(e);
                    }
                    if (!Modifier.isAbstract(c.getModifiers())) {
                        classes.add(c);
                    }
                }
                return classes;
            }
        }
    
    }
    

提交回复
热议问题