The Artima Developer Community
Sponsored Link

Drive Your Unit Tests with Custom Scripts
How to Customize Suite Execution by Overriding execute
by Bill Venners
March 10, 2003

<<  Page 7 of 11  >>

Advertisement

Overriding execute

Class Suite's implementation of execute simply invokes executeTestMethods and executeSubSuites, in that order. Suite's executeTestMethods implementation discovers test methods through reflection and invokes them. Suite's executeSubSuites implementation invokes execute on each of its sub-Suites. ScriptDrivenAccountSuite execute method provides an alternate way to execute its own tests, but uses the default way of executing sub-Suites. ScriptDrivenAccountSuite's execute method:

Here's ScriptDrivenAccountSuite's execute method:

    /**
     * Execute this suite object.
     *
     * <P>This class's implementation of this method
     * executes the test commands contained in the script file,
     * then invokes <code>executeSubSuites</code> on itself,
     * passing in the specified <code>Reporter<code>.
     *
     * @param reporter the <code>Reporter</code> to which results will be reported
     * @exception NullPointerException if <CODE>reporter</CODE> is <CODE>null</CODE>.
     */
    public void execute(Reporter reporter) {

        if (reporter == null) {
            throw new NullPointerException("reporter is null");
        }

        BufferedReader reader = null;
        try {
            FileInputStream fis = new FileInputStream(scriptFileName);
            InputStreamReader isr = new InputStreamReader(fis);
            reader = new BufferedReader(isr);
        }
        catch (FileNotFoundException e) {
            // Runner will report this RuntimeException as with suiteAborted
            // method invocation on the Reporter
            throw new RuntimeException("Unable to open " + scriptFileName
                + ". " + e.getMessage());
        }

        try {
            int lineNum = 1;
            String line = reader.readLine();
            while (line != null) {
                if (isStopRequested()) {
                    return;
                }
                processLine(reporter, line, lineNum);
                line = reader.readLine();
                ++lineNum;
            }
        }
        catch (IOException e) {
            // Runner will report this RuntimeException as with suiteAborted
            // method invocation on the Reporter
            throw new RuntimeException("Unable to read a line from script file "
                + scriptFileName + ". " + e.getMessage());
        }
        finally {

            try {
                reader.close();
            }
            catch (IOException e) {
                throw new RuntimeException("Unable to close script file " +
                    scriptFileName + ". " + e.getMessage());
            }
        }

        executeSubSuites(reporter);
    }

<<  Page 7 of 11  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us