Class JUnitTestRunner

  • All Implemented Interfaces:
    JUnitTaskMirror.JUnitTestRunnerMirror, junit.framework.TestListener

    public class JUnitTestRunner
    extends java.lang.Object
    implements junit.framework.TestListener, JUnitTaskMirror.JUnitTestRunnerMirror
    Simple Testrunner for JUnit that runs all tests of a testsuite.

    This TestRunner expects a name of a TestCase class as its argument. If this class provides a static suite() method it will be called and the resulting Test will be run. So, the signature should be

     <code>
         public static junit.framework.Test suite()
     </code>
     

    If no such method exists, all public methods starting with "test" and taking no argument will be run.

    Summary output is generated at the end.

    Since:
    Ant 1.2
    • Constructor Summary

      Constructors 
      Constructor Description
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure)
      Constructor for fork=true or when the user hasn't specified a classpath.
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure, boolean showOutput)
      Constructor for fork=true or when the user hasn't specified a classpath.
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure, boolean showOutput, boolean logTestListenerEvents)
      Constructor for fork=true or when the user hasn't specified a classpath.
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure, boolean showOutput, boolean logTestListenerEvents, java.lang.ClassLoader loader, boolean suppressJunitTenant)
      Constructor to use when the user has specified a classpath.
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure, boolean showOutput, java.lang.ClassLoader loader)
      Constructor to use when the user has specified a classpath.
      JUnitTestRunner​(java.lang.String platformHome, java.lang.String classpath, java.lang.String webClasspath, JUnitTest test, boolean haltOnError, boolean filtertrace, boolean haltOnFailure, java.lang.ClassLoader loader)
      Constructor to use when the user has specified a classpath.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addError​(junit.framework.Test test, java.lang.Throwable t)
      Interface TestListener.
      void addFailure​(junit.framework.Test test, java.lang.Throwable t)
      Interface TestListener for JUnit <= 3.4.
      void addFailure​(junit.framework.Test test, junit.framework.AssertionFailedError t)
      Interface TestListener for JUnit > 3.4.
      void addFormatter​(JUnitResultFormatter f)
      Add a formatter.
      void addFormatter​(JUnitTaskMirror.JUnitResultFormatterMirror f)
      Add a formatter to the test.
      void endTest​(junit.framework.Test test)
      Interface TestListener.
      static java.lang.String filterStack​(java.lang.String stack)
      Filters stack frames from internal JUnit and Ant classes
      static java.lang.String getFilteredTrace​(java.lang.Throwable t)
      Returns a filtered stack trace.
      int getRetCode()
      Returns what System.exit() would return in the standalone version.
      void handleErrorFlush​(java.lang.String output)
      Handle output sent to System.err.
      void handleErrorOutput​(java.lang.String output)
      Handle output sent to System.err.
      void handleFlush​(java.lang.String output)
      Handle output sent to System.out.
      int handleInput​(byte[] buffer, int offset, int length)
      Handle input.
      void handleOutput​(java.lang.String output)
      Handle a string destined for standard output.
      static void main​(java.lang.String[] args)
      Entry point for standalone (forked) mode.
      void run()
      Run the test.
      void setPermissions​(org.apache.tools.ant.types.Permissions permissions)
      Permissions for the test run.
      void startTest​(junit.framework.Test t)
      Interface TestListener.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • suppressJunitTenant

        protected boolean suppressJunitTenant
    • Constructor Detail

      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure)
        Constructor for fork=true or when the user hasn't specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure,
                               boolean showOutput)
        Constructor for fork=true or when the user hasn't specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
        showOutput - whether to send output to System.out/.err as well as formatters.
      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure,
                               boolean showOutput,
                               boolean logTestListenerEvents)
        Constructor for fork=true or when the user hasn't specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
        showOutput - whether to send output to System.out/.err as well as formatters.
        logTestListenerEvents - whether to print TestListener events.
        Since:
        Ant 1.7
      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure,
                               java.lang.ClassLoader loader)
        Constructor to use when the user has specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
        loader - the classloader to use running the test.
      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure,
                               boolean showOutput,
                               java.lang.ClassLoader loader)
        Constructor to use when the user has specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
        showOutput - whether to send output to System.out/.err as well as formatters.
        loader - the classloader to use running the test.
      • JUnitTestRunner

        public JUnitTestRunner​(java.lang.String platformHome,
                               java.lang.String classpath,
                               java.lang.String webClasspath,
                               JUnitTest test,
                               boolean haltOnError,
                               boolean filtertrace,
                               boolean haltOnFailure,
                               boolean showOutput,
                               boolean logTestListenerEvents,
                               java.lang.ClassLoader loader,
                               boolean suppressJunitTenant)
        Constructor to use when the user has specified a classpath.
        Parameters:
        test - the test to run.
        haltOnError - whether to stop the run if an error is found.
        filtertrace - whether to filter junit.*.* stack frames out of exceptions
        haltOnFailure - whether to stop the run if failure is found.
        showOutput - whether to send output to System.out/.err as well as formatters.
        logTestListenerEvents - whether to print TestListener events.
        loader - the classloader to use running the test.
        suppressJunitTenant - whether junit tenant should be started or not
        Since:
        Ant 1.7
    • Method Detail

      • getRetCode

        public int getRetCode()
        Returns what System.exit() would return in the standalone version.
        Specified by:
        getRetCode in interface JUnitTaskMirror.JUnitTestRunnerMirror
        Returns:
        2 if errors occurred, 1 if tests failed else 0.
      • startTest

        public void startTest​(junit.framework.Test t)
        Interface TestListener.

        A new Test is started.

        Specified by:
        startTest in interface junit.framework.TestListener
        Parameters:
        t - the test.
      • endTest

        public void endTest​(junit.framework.Test test)
        Interface TestListener.

        A Test is finished.

        Specified by:
        endTest in interface junit.framework.TestListener
        Parameters:
        test - the test.
      • addFailure

        public void addFailure​(junit.framework.Test test,
                               java.lang.Throwable t)
        Interface TestListener for JUnit <= 3.4.

        A Test failed.

        Parameters:
        test - the test.
        t - the exception thrown by the test.
      • addFailure

        public void addFailure​(junit.framework.Test test,
                               junit.framework.AssertionFailedError t)
        Interface TestListener for JUnit > 3.4.

        A Test failed.

        Specified by:
        addFailure in interface junit.framework.TestListener
        Parameters:
        test - the test.
        t - the assertion thrown by the test.
      • addError

        public void addError​(junit.framework.Test test,
                             java.lang.Throwable t)
        Interface TestListener.

        An error occurred while running the test.

        Specified by:
        addError in interface junit.framework.TestListener
        Parameters:
        test - the test.
        t - the error thrown by the test.
      • setPermissions

        public void setPermissions​(org.apache.tools.ant.types.Permissions permissions)
        Permissions for the test run.
        Specified by:
        setPermissions in interface JUnitTaskMirror.JUnitTestRunnerMirror
        Parameters:
        permissions - the permissions to use.
        Since:
        Ant 1.6
      • handleInput

        public int handleInput​(byte[] buffer,
                               int offset,
                               int length)
                        throws java.io.IOException
        Handle input.
        Specified by:
        handleInput in interface JUnitTaskMirror.JUnitTestRunnerMirror
        Parameters:
        buffer - not used.
        offset - not used.
        length - not used.
        Returns:
        -1 always.
        Throws:
        java.io.IOException - never.
        Since:
        Ant 1.6
        See Also:
        Task.handleInput(byte[], int, int)
      • addFormatter

        public void addFormatter​(JUnitResultFormatter f)
        Add a formatter.
        Parameters:
        f - the formatter to add.
      • main

        public static void main​(java.lang.String[] args)
                         throws java.io.IOException
        Entry point for standalone (forked) mode.

        Parameters: testcaseclassname plus parameters in the format key=value, none of which is required.

        key description default value
        haltOnError halt test on errors? false
        haltOnFailure halt test on failures? false
        formatter A JUnitResultFormatter given as classname,filename. If filename is ommitted, System.out is assumed. none
        showoutput send output to System.err/.out as well as to the formatters? false
        logtestlistenerevents log TestListener events to System.out. false
        Parameters:
        args - the command line arguments.
        Throws:
        java.io.IOException - on error.
      • getFilteredTrace

        public static java.lang.String getFilteredTrace​(java.lang.Throwable t)
        Returns a filtered stack trace. This is ripped out of junit.runner.BaseTestRunner.
        Parameters:
        t - the exception to filter.
        Returns:
        the filtered stack trace.
      • filterStack

        public static java.lang.String filterStack​(java.lang.String stack)
        Filters stack frames from internal JUnit and Ant classes
        Parameters:
        stack - the stack trace to filter.
        Returns:
        the filtered stack.