четверг, 24 января 2013 г.

Thoughts after reading "Practical TDD and Acceptance TDD for developers"

TDD for web applications

After reading the book «Practical TDD and Acceptance TDD fot developers» I decided to write some thoughts about testing.

TDD Steps

  1. Write the Test.
  2. Write the code that will pass the test.
  3. Refactor the test and the code. Watch carefully what dependencies can be eliminated or restructed.
The real question is how to write code that can be tested? Not how to test the written code. If code can't be tested — nobody needs such code.

Therefore when writing the code one should rely on the technical solutions that allow you to test the code.

Testing servlets

There is no universal solution to testing the servlets.

The main obstacle — how to make servlet that it could support dependency injection.
Following choices are available:
  • use the interface HttpRequestHandler from Spring library,
  • use methods get and set for setting the dependencies.

Unit testing database access code

How to test jdbc? One can test the code that use the services to access the data or the code that directly work with database.
One can use spring jdbctemplate or hibernate. Along with it you can use EasyMock for testing the code, that access the database.

Hibernate allows you to offload from controlling the differencies in SQL among the databases. Also it looks for opening and closing the resources

Testing database access code in memory database

Testing in HSQLDB. Hibernate settings:
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:mem:testdb
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

Automating database schema configuration

Solutions: http://dbdeploy.com/ , http://www.liquibase.org/.
Testing Database access code: http://dbunit.sf.net .

Testing code working with files

For working with files it is better to use http://commons.apache.org/vfs/.
One can use RAM filesystem for unit testing.

Testing code dealing with time

One of the solutions is to create your onw interface and substitute different implementations on the demand. Also you can take a look at joda-time library. Also watch for the strategy pattern.

четверг, 17 января 2013 г.

Axis 2 basic authentication on client

Some time ago I need to connect to web-service which use HTTP basic authentication. I had an Axis 2 client. So, here what I did:
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setPreemptiveAuthentication(true);
auth.setUsername(login);
auth.setPassword(password);
stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE,auth); 

Now you can connect to protected services.

вторник, 1 января 2013 г.