Xebia

Blog Xebia France

J2EE, Agility and SOA

October 2nd, 2009

Light on JTESTR

Among the inevitable tasks of the life of a developer, there is the writing of the unit tests. Many tools try to make life easier for us on this point. Today, I am going to speak to you of , a framework of unit tests who brings power and speed of writing of the scripting to test Popular dance applications. Thrown by Ola Bini, an inevitable contributor the plan JRUBY, JTESTR is directly integrable with Ant, Buildr and Maven 2. This plan is not still very spread, but it brings advantages which are worth being studied.

Skyline tower

JTESTR is group of bookshops Ruby devoted to tests, alloyed in JRUBY to allow a quick starting and without pain of the writing of tests. One of the main trumps of the scripting is the speed of writing, and Ruby is a language of scripting become very popular these last years, notably thanks to Ruby one Rails. The unit tests and of integration make integral part of the world Ruby and several tools came into the world to make them easier. JTESTR loads the bookshops of test Ruby following:

As tool Popular dance, he also allows correlations with frameworks:

In its last version, JTESTR requires the use of Popular dance 1.6.

Integration

Delivered in the form of a jar with a task Ant, it is easily integrable in an existent plan. Once the available jar in your classpath, it is enough to add a beacon taskdef in your build for more comfort and the turn is played:

<target name = "test" description = "Runs all tests with default shape of JTESTR">
 <taskdef name = " Jtestr " classname = "org.jtestr.ant. JtestRAntRunner" classpath = "Lib / jtestr.jar"/>

 <JTESTR/>
</target>

For plans Maven, a modification minor of the pom will suit:

<! - in the section plugins->
 <plugin>
  <groupId> org.jtestr </groupId>
  <artifactId> jtestr </artifactId>
  <version 0.4.0> </version>
  <shape>
   <tests> src / test / ruby </tests>
  </shape>
  <executions>
   <execution>
    <goalkeepers>
     <goalkeeper> test </goalkeeper>
    </goalkeepers>
   </execution>
  </executions>
 </plugin>

The directive of shape tests allows to specify the directory in which to search the scripts of tests. To keep mind Maven, I recommend you to create a directory src/test/ruby and to put all tests there. The hierarchy in pence directories to represent the packages of classes to be tested does not put down worries.

Writing of tests

JTESTR gives several possibilities for the writing of tests. First of all, the use of the framework of Ruby's academic tests:Test::Unit. It does not bring tests JUNIT from big novelty in comparison with classics. The Swiss knife of method assert am available similarly.

Example of Test:: Joins

class HashMapTests <Test:: Joins TestCase::
 def setup
  @map = java.util. HashMap.new
 end

 def test_that_map_is_empty
  assert @map.isEmpty
 end

 def test_that_it_returns_a_keyset_that_returns_an_iterator_that_throws_exception
  assert_raises (java.util. NOSUCHELEMENTEXCEPTION) do
   @map.key_set.iterator.next
  end
 end
end

But it is also possible to use RSPEC. This framework of tests is orientated "specification" and offers an original notation which tends to bring closer to the writing of the tests of the specification of waited behaviour. Compared notation:

# with Test:: Joins
assert_equals (result, 42)

# with RSPEC
result.should == 42

They get closer so to the natural language. In English in the text, certainly, but they get closer. The structure of a complete file RSPEC gives for example:

java.util import. HashMap

describe "a Year empty", HashMap do
 before:each do
  @hash_map = HashMap.new
 end

 it "should not be empty After a year entry has been added to it" do
  @hash_map.put "Foo", "bar"
  @hash_map.should_not be_empty
 end

 it " should be empty " do
  @hash_map.should be_empty
  @hash_map.size.should == 0
 end

 it "should return has exception keyset iterator that throws a year they next" do
  proc do
   @hash_map.key_set.iterator.next
  end.should raise_error (java.util. NOSUCHELEMENTEXCEPTION)
 end
end

It is possible to mix Test in the same plan of tests:: Joins tests RSPEC and. The respect for a norm of naming of files is enough in JTESTR to determine the directed framework.

An important precision on an obscure point in material: order import search in beginning packages by java, javax, org and com. For all the others it is necessary to write:

import Popular dance:: fr.votre.package. VotreClasse

Limitations

A problem which can be checked off of the finger is the time of load of environment JRUBY. The interpreter indeed takes sometimes more than 10 seconds to launch. To overcome this problem, JTESTR has a mode server which allows to keep a server in thorough task with environment JRUBY in waiting of tests to be thrown. The time of load is easily abolished so.

The training of the bases of language Ruby can also cast down, but it is a weak obstacle as much as it is a language entirely object and where classes Popular dance of plan are predominantly manipulated to be tested. The step is rather easily generally crossed.

Conclusions

The offer of frameworks of tests is already consequent in the world Popular dance. JTESTR is not very spread at present. Nevertheless, the use of RSPEC for the writing of tests allows to remain close to a verbal formulation of waited behaviours. In a step TDD, it can be favourable. They will have so tendency to avoid getting lost in technical formulations from the first stages of plan.

Besides, I will take JTESTR into consideration seriously if I had developers Ruby in my team. The framework gives you a chance to break border between your teams Ruby et Java. The very concise aspect of tests makes them legible as much by the developers Popular dance as Ruby and reinforces mutual assistance between teams. It makes easier the distribution of competences and of knowledge between developers, what is never a bad thing.

Resources:

Key words :, , , ,

You can follow answers accepted by this article thanks to the thread of comments.

Leave a comment

Xebia it Architects lock France
Defence Colis e - 10/12, avenue of The Ark
92419 Courbevoie Cedex

T l : +33 (0) 1 46 91 76 16
Fax : +33 (0) 1 46 91 88 00
E-mail : info@xebia.fr