Chapter 7. Testing

Introduction

The focus of this chapter is the topic of automated testing, specifically, testing Java code with Ruby. There are several key advantages of using a dynamic language, such as Ruby, to test code written in a statically typed language, such as Java:

  • Automated test cases tend to require a lot of bootstrapping code. Using a domain-specific language (DSL) such as those provided by the Ruby frameworks like dust and Expectations can cut down on this repetitive code.

  • Dynamic languages make it very easy to create mock objects. JRuby, for example, allows you to directly instantiate Java interfaces.

  • Open classes allow code to be modified at runtime to facilitate testing.

There are a variety of testing frameworks available in Ruby, the most popular of which are wrapped into a JRuby-based project called JtestR. JtestR is an open source project that Ola Bini and Anda Abramovici, developers at ThoughtWorks, started in 2008 with the purpose of making it easy to test Java code with a variety of Ruby testing frameworks. As of the current version 0.3, JtestR includes support for:

  • Test/Unit

  • RSpec

  • Expectations

  • dust

  • Mocha

In addition, JtestR supports the Java testing frameworks JUnit and TestNG, making it a “one-stop shop” for testing frameworks.

JtestR is available for download from http://jtestr.codehaus.org.

Unit Testing Java Code with Test/Unit

Problem

You want to test your Java code using a more concise syntax than is available from Java testing frameworks such as JUnit and ...

Get JRuby Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.