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 3 of 11  >>

Advertisement

A Test Scripting Language for Class Account

ScriptDrivenAccountSuite tests class Account, a simple example class included in the Artima SuiteRunner distribution ZIP file. Once you unzip the distribution ZIP file, you'll find the source code for ScriptDrivenAccountSuite in the suiterunner-[release]/example/com/artima/examples/scriptdriven/ex1 directory. You can also view the complete listing in HTML. Because ScriptDrivenAccountSuite.java is released under the Open Software License, you can use it as a template when overriding execute your own custom Suite subclass.

Aside from a no-arg constructor that initializes the account balance to zero, class Account has three methods in the public interface: deposit, withdraw, and getBalance. The contract for these methods is shown in Figure 1.

Figure 1. The Account Class

com.artima.examples.account.ex6
Account
public class Account
    Represents a bank account.
Constructors
public Account()
    Construct a new Account with a zero balance.
Methods
public void deposit(long amount)
    Deposits exactly the passed amount into the Account.
public long getBalance()
    Gets the current balance of this Account.
public long withdraw(long amount) throws InsufficientFundsException
    Withdraws exactly the passed amount from the Account.

Class ScriptDrivenAccountSuite consumes a test script, a file containing a sequence of commands for testing class Account. Each line of the file can contain at most one test command, and each test command can occupy only a single line. Blank lines are ignored. A pound sign (#), and any characters appearing after it to the end of the line, are also ignored, allowing for comments.

The four test commands are:

The newAccount Command

The newAccount commands causes ScriptDrivenAccountSuite to:

Here's an example of a newAccount command in a script file:
newAccount

The getBalance Command

The getBalance command takes one argument, a long expected return value. This command causes ScriptDrivenAccountSuite to:

Here's an example of a getBalance command in a script file:

getBalance 100

The deposit Command

The deposit command has two forms. The first form takes one argument, a long amount to deposit. This command will cause ScriptDrivenAccountSuite to:

Here's an example of the first form of the deposit command in a script file:

deposit 10

The second form of the deposit command has two arguments, a long amount to deposit and the fully qualified name of an expected exception. This command will cause ScriptDrivenAccountSuite to:

Here's an example of the second form of the deposit command in a script file:

deposit 1000 java.lang.ArithmeticException

The withdraw Command

Like deposit, the withdraw command has two forms. The first form takes one argument, a long amount to withdraw. This command will cause ScriptDrivenAccountSuite to:

Here's an example of the first form of the withdraw command in a script file:

withdraw 20

The second form of the withdraw command has two arguments, a long amount to withdraw and the fully qualified name of an expected exception. This command will cause ScriptDrivenAccountSuite to:

Here's an example of the second form of the withdraw command in a script file:

withdraw 10 com.artima.examples.account.ex6.InsufficientFundsException

<<  Page 3 of 11  >>


Sponsored Links



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