Test code generation

It's easy to test your amalegeni code now, without having to write tons of java statements, just apply the

test
{{
}}

statement to your service methods. This page shows you how.

New

What's new in amalegeni v.1.0.10 (released in January 2012) is that the cs.stg and csae.stg templates now also generate a separate service class containing test code that calls your service methods. You just provide some simple initialization code in your amalgeni file, and that's it. After running amalegeni on it, just execute the generated Test.class.

Next to that, another improvement for this amalegeni release is that for the bean code a default toString() method is generated, which can be easily modified with your custom code.

Example

Here's an example, which you can download at the end of this page.

Have a look at this service method, which must look familiar to you now if you've done a bit of amalgeni coding:

List<AirportBean> getAirportList(String regionLike) 
<< 
    select iata,icao,city_served,region,airport_name
    from   t_airport
    where  region like ?
>>

How do you put in the test code? Just add the test-statement at the end, and in between the double curly braces you put java code that populates the arguments of your method. In this case there is only one argument, so we just put ..

String regionLike="%ca%";

Be sure that the name and type of the parameter corresponds to the one used in your method call.

Gluing it all together gives:

service AirportService extends PostgresqlBaseService target postgresql
{

    List<AirportBean> getAirportList(String regionLike) 
    << 
        select iata,icao,city_served,region,airport_name
        from   t_airport
        where  region like ?
    >>
    test
    {{  
        String regionLike="%ca%";
    }}
    
}

Run amalegeni on that (with the latest cs.stg template), and then execute the generated test class file. amalegenisample.s03simpletest.generated.AirportServiceTest

Your output should look like this:

Running test_getAirportList()
* List<AirportBean> getAirportList(String) returned: 
String iata=TNM
String icao=SCRM
String cityServed=Antarctica
String region=Magallanes & Antártica
String airportName=Teniente Rodolfo Marsh Martin Base

String iata=ARI
String icao=SCAR
String cityServed=Arica
String region=Arica & Parinacota
String airportName=Chacalluta International Airport

String iata=SMB
String icao=SCSB
String cityServed=Cerro Sombrero
String region=Magallanes & Antártica
String airportName=Franco Bianco Airport

String iata=CNR
String icao=SCRA
String cityServed=Chanaral
String region=Atacama
String airportName=Chanaral Airport

String iata=CPO
String icao=SCHA
String cityServed=Copiapó
String region=Atacama
String airportName=Chamonate Airport
..
..

Custom toString() for your bean

If you want to improve the output, eg. putting every record on 1 line instead, then we need to add custom toString code to the bean. This is done by appending a toString section to the bean, as follows:

bean AirportBean
{
  String iata;  
  String icao;   
  String cityServed;   
  String region;   
  String airportName;
}
toString
{{
    return String.format("%4s|%5s|%-30s|%-30s|%s)"
            ,iata,icao,cityServed,region,airportName
            );
}}

This toString custom code can contain as many lines as you want, it's not limited to just one line, but make sure that the final statement returns a string.

So compile your code and run it again, and now you'll get a better looking output. Simple no?

Running test_getAirportList()
* List<AirportBean> getAirportList(String) returned: 
 TNM| SCRM|Antarctica                    |Magallanes & Antartica        |Teniente Rodolfo Marsh Martin Base)
 ARI| SCAR|Arica                         |Arica & Parinacota            |Chacalluta International Airport)
 SMB| SCSB|Cerro Sombrero                |Magallanes & Antartica        |Franco Bianco Airport)
 CNR| SCRA|Chanaral                      |Atacama                       |Chanaral Airport)
..
..

If you look at the generated test code, it looks like this:

    public static void test_getAirportList() throws SQLException
    {
        AirportService service=new AirportServiceImpl();

        String regionLike="%ca%";

        List<AirportBean> _returnedCollection = service.getAirportList(regionLike);
        System.out.println("* List<AirportBean> getAirportList(String) returned: " );
        for ( AirportBean _returnedElement : _returnedCollection )
        {
            System.out.println( _returnedElement.toString());
        }
        System.out.println("------");
    }

On line 5 you find the asssignment provided by you in the amalegeni code.

Download the samplecode

Here's all the code for the above sample: teststatement.zip .

Some notes:

  • you are assumed to have a postgres database running, that has a user 'alisample' with password 'alisample'. You can create the user like this on linux:
    $ sudo -u postgres psql template1
    template1=# create user alisample encrypted password 'alisample' ;
    template1=# create database alisample with encoding 'UNICODE' owner alisample;
    template1=# \q
  • download the above zipfile and unzip it
  • adapt the build.xml so that the path-element for the jdbc jar points to your postrgresql jdbc jar file:
    <pathelement path="/opt/jdbc/postgres/postgresql-8.3-604.jdbc4.jar"/>
  • run the setup (create + populate table), actual test and teardown using this command:
    ant runS03
  • if you don't want to use ant, then to setup the table + data just run class amalegenisample.s03simpletest.generated.SetupServiceTest
  • if you want to clean up at the end, then drop the table t_airport by running amalegenisample.s03simpletest.generated.TeardownServiceTest

One remark: the method test statement, and the bean custom toString statement are provided by the cs.stg and for the csae.stg template, not by the gwt.stg template (in fact, I fear the gwt.stg template is getting a bit out of date).

Written by Willem on 20120114

© Willem Moors, 2009 - 2013