Testing Java Applications using Groovy blocks/en: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
Zeile 60: Zeile 60:


[[Java Browser/en]] plugin for expecco provides a simple browser to load and browse Java code directly from expecco environment.
[[Java Browser/en]] plugin for expecco provides a simple browser to load and browse Java code directly from expecco environment.
To open a Java Browser on "Bank Account" code, select ''Extras'' ► ''Plugins'' ► ''Java Browser'' ► ''Open...'' from expecco menu
To open a Java Browser on "Bank Account" code, select ''Extras'' ► ''Plugins'' ► ''Java Browser'' ► ''Open...'' from expecco menu and define a new Java browser workspace. A Java browser workspace is a folder where Java Browser stores all the Java code
and define a new Java browser workspace.
and sources. You can define as many workspaces as you like and

A Java browser workspace is a folder where Java Browser stores all the Java code and sources. You can define as many workspaces as you like and
switch between them. If you don't have a workspace for "Bank Account" yet, just enter an empty directory and new workspace will be created.
switch between them. If you don't have a workspace for "Bank Account" yet, just enter an empty directory and new workspace will be created.
Initially the workspace is empty, so to actually see the code of "Bank Account", we have add it's code and sources to the workspace. To do so,
Initially the workspace is empty, so to actually see the code of "Bank Account", we have add it's code and sources to the workspace. To do so,
Zeile 72: Zeile 70:
[[Datei:expecco_BankAccounts_005.png]]
[[Datei:expecco_BankAccounts_005.png]]


Let's implement the ''Create 2 accounts & make a transaction'' block now. Just create a new Groovy block and
write the code. To make writing code a little bit easier, [[SmallSense/en]] plugin together with [[Java Browser/en]]
plugin provides a simple completion support for Groovy. The completion engine takes information about available classes
and methods from the Java workspace. In other words, if you have no Java browser workspace opened, the completion won't
suggest any classes nor methods.

To trigger the completion, just press ''Ctrl-Space'':


[[Datei:expecco_BankAccounts_006.png]]




The code of the block may be the following:




import exept.expecco.projects.examples.bankaccount.Bank;
import exept.expecco.projects.examples.bankaccount.Account;
// end of definitions -- do not remove this line unless imports above is empty
def execute() {
Bank b = new Bank( "Some bank" );
Account account1 = new Account( "Owner 1" );
b.addAccount( account1 );
account1.setBalance( initialBalance1.value() );
Account account2 = new Account( "Owner 2" );
b.addAccount( account2 );
account2.setBalance( initialBalance2.value() );
account1.credit(account2, amount.value() );
balance1.value( account1.getBalance() );
balance2.value( account2.getBalance() );
}





Version vom 30. September 2014, 15:46 Uhr

This example demonstrates how to use Java Browser/en, Java Debugger/en and SmallSense/en plugins for Java application test development. It shows how to use Groovy blocks to

The Bank Account Example[Bearbeiten]

Through this tutorial, we'll use a simple "Bank Account" as system under test. "Bank Account" is a very simple Java library that models banks and accounts and allow transation among accounts.

The code of the "Bank Account" application is not correct at place bu purpose. You may download a full Eclipse project containing the example code at somewhere.

Setting up expecco[Bearbeiten]

For the sake of brevity, in this tutorial we'll use simple local JVM managed by expecco to run "Bank Account"s code. As for any Java application, we have to set up a classpath properly so the JVM can find application code.

Let's define an expecco environment variable named CLASSPATH_BANKACCOUNT defining where the "Bank Account" code is actually located:

Expecco BankAccounts 001.png


Then let's define a "SetUp" block that actually adds the directory to JVM class path. A simple JavaScript block would do it:

execute

   JAVA::Java singletonInstance
       addToClassPath: (self environmentAt: 'CLASSPATH_BANKACCOUNT')    

This block has to be run before any other Java code using "Bank Account" classes is run. Putting it as test plan's pre-execution block !ref! might be a good way to ensure it.

Designing a test[Bearbeiten]

Once the class path is set up, we start designing an implementing tests. Let's start with a simple tests that create a two bank accounts and then make transfer from one account to another. Then tests would check if the final balance on accounts is what one would expect.

The very simple test would be then:

  • create account A1 with initial balance 1000.
  • create account A2 with initial balance 1000.
  • transfer 500 from account A2 to account A1.
  • check that final balance of A1 is 1500
  • check that final balance of A2 is 500.

In expecco, the testcase may look like:

Expecco BankAccounts 002.png

The block Create 2 accounts & make a transaction would actually call the Java API of a "Bank Account" application. By separating the actual action code from assertions (check) we can easily create more tests just with different values. This will become handy later.

Calling the Java API[Bearbeiten]

In order to implement the test as outlined above, we have to implement a block that actually call the Java API.

Java Browser/en plugin for expecco provides a simple browser to load and browse Java code directly from expecco environment. To open a Java Browser on "Bank Account" code, select ExtrasPluginsJava BrowserOpen... from expecco menu and define a new Java browser workspace. A Java browser workspace is a folder where Java Browser stores all the Java code and sources. You can define as many workspaces as you like and switch between them. If you don't have a workspace for "Bank Account" yet, just enter an empty directory and new workspace will be created. Initially the workspace is empty, so to actually see the code of "Bank Account", we have add it's code and sources to the workspace. To do so, just click to Open Settings button in Java Browser window or select WorkspaceSettings from window menu. In Settings dialog, click to Add Library button and add Bank Account code. One added and confirmed (by pressing OK button), you may browse through the code, check what classes and methods are available and so on.

Expecco BankAccounts 005.png

Let's implement the Create 2 accounts & make a transaction block now. Just create a new Groovy block and write the code. To make writing code a little bit easier, SmallSense/en plugin together with Java Browser/en plugin provides a simple completion support for Groovy. The completion engine takes information about available classes and methods from the Java workspace. In other words, if you have no Java browser workspace opened, the completion won't suggest any classes nor methods.

To trigger the completion, just press Ctrl-Space:

Expecco BankAccounts 006.png


The code of the block may be the following:


   import exept.expecco.projects.examples.bankaccount.Bank;
   import exept.expecco.projects.examples.bankaccount.Account;
   // end of definitions -- do not remove this line unless imports above is empty
   def execute() {
       Bank b = new Bank( "Some bank" );
       Account account1 = new Account( "Owner 1" );
       b.addAccount( account1 );
       account1.setBalance( initialBalance1.value() );
       Account account2 = new Account( "Owner 2" );
       b.addAccount( account2 );
       account2.setBalance( initialBalance2.value() );
       account1.credit(account2, amount.value() ); 
       balance1.value( account1.getBalance() );
       balance2.value( account2.getBalance() );
  }               


To remove[Bearbeiten]

java -agentlib:jdwp=transportt_socket,server=y,suspend=n,address=3003 \
     -cp bin \
     -jar "/home/jv/Private/Projects/SmalltalkX/sources/branches/jv1/build/exept/technologyBridge/javaBridge/javaBridge_Server_Client/agents/JavaBridge.jar"\
     -port 5005 -asServer -localOnly \
     -extDir "/home/jv/Private/Projects/SmalltalkX/sources/branches/jv1/build/exept/technologyBridge/javaBridge/javaBridge_Server_Client/ext"



Copyright © 2014-2024 eXept Software AG