Example usage for junit.framework TestResult errorCount

List of usage examples for junit.framework TestResult errorCount

Introduction

In this page you can find the example usage for junit.framework TestResult errorCount.

Prototype

public synchronized int errorCount() 

Source Link

Document

Gets the number of detected errors.

Usage

From source file:grails.util.RunTests.java

public static void main(String[] args) {
    int exitCode = 0;
    try {/*w w  w. j  a  v a 2  s.c o m*/
        log.info("Bootstrapping Grails from classpath");
        ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext) GrailsUtil
                .bootstrapGrailsFromClassPath();
        GrailsApplication application = (GrailsApplication) appCtx.getBean(GrailsApplication.APPLICATION_ID);

        Class[] allClasses = application.getAllClasses();
        log.debug("Going through [" + allClasses.length + "] classes");
        TestSuite s = new TestSuite();
        for (int i = 0; i < allClasses.length; i++) {
            Class clazz = allClasses[i];
            if (TestCase.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
                log.debug("Adding test [" + clazz.getName() + "]");
                s.addTest(new GrailsTestSuite(appCtx, clazz));
            } else {
                log.debug("[" + clazz.getName() + "] is not a test case.");
            }
        }
        String[] beanNames = appCtx.getBeanNamesForType(PersistenceContextInterceptor.class);
        PersistenceContextInterceptor interceptor = null;
        if (beanNames.length > 0) {
            interceptor = (PersistenceContextInterceptor) appCtx.getBean(beanNames[0]);
        }

        try {
            if (interceptor != null) {
                interceptor.init();
            }
            TestResult r = TestRunner.run(s);
            exitCode = r.errorCount() + r.failureCount();
            if (exitCode > 0) {
                System.err.println("Tests failed!");
            }
            if (interceptor != null)
                interceptor.flush();
        } finally {
            if (interceptor != null)
                interceptor.destroy();
        }
    } catch (Exception e) {
        log.error("Error executing tests: " + e.getMessage(), e);
        exitCode = 1;
    } finally {
        System.exit(exitCode);
    }
}