Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Learn Scala with Specs2 Spring

Use Scala code to safely test and debug Spring-based Java apps

  • Print
  • Feedback

Using Specs2 Spring to test your Java apps is an efficient and safe way to learn the patterns of object-functional programming with Scala: you get the benefits of Specs2 (a Scala-based open source testing framework) without leaving the Spring framework or rewriting perfectly good Java code. Specs2 Spring creator Jan Machachek walks through three testing examples, including a short introduction to acceptance testing, which will be integrated in the forthcoming Specs2 Spring v1.0.

The Spring framework in its early days revolutionized Java enterprise application development. Lightweight and sensible, Spring simplified the boilerplate code that made Java programming time-consuming and tedious. It abstracted the details of essential Java libraries so that developers could just use them in their programs. It made Java programming fun again, and it did so at a time when many Java developers had become resigned to complexity. Those were the days of Java 1.4.

Today, the lightweight programming paradigm that Spring popularized has become nearly a gold standard for Java enterprise tools and frameworks. Spring has become less of a radical alternative and more of a reliable workhorse, used by hundreds of thousands of software developers. Collectively, we've invested millions of lines of code into our Spring-based applications, and we'll write millions more. But we might not be writing our next applications in Java code.

This article is for the Java developer who wants to learn Scala, but doesn't want to rewrite existing Java apps or leave the Spring framework to do it. I'll briefly discuss the benefits of writing Spring components in Scala, and then introduce the Specs2 test framework and Specs2 Spring -- an extension I wrote for Cake Solutions, where we use it to introduce Scala code to Spring-based Java applications. I'll walk through several scenarios of using Scala to test Java code, including a unit-testing example and an integration-testing example. I'll conclude with a brief demonstration of acceptance testing using Specs2 Spring, which will be part of the forthcoming Spec2 Spring v 1.0 release.

Note that this article is intended for developers familiar with the Spring framework.

Spring and Scala

As a Spring developer, you probably know that there are advantages to integrating Scala with Spring, and also some challenges. For one thing, Scala rejects Spring's dependency injection model. In Scala, we do not inject instances of other objects into existing components but compose functionality. Scala replaces Spring's dependency injection with composition inheritance. But as a strongly-typed object-functional language, Scala offers much more than just a different approach to dependency injection. Scala lets you construct rich type systems and leverage functional programming, while still doing object-oriented programming. With Scala, you can express concepts that are difficult or impossible to implement in Java code, an increasingly important advantage for developers building (for instance) highly concurrent applications.

  • Print
  • Feedback

Resources
  • Specs2 Spring is a Specs2 extension for BDD & testing typical Spring enterprise applications in Scala.
  • The Spring framework is a de-facto standard for implementing loosely-coupled, flexible, and testable Java EE applications.
  • Specs2 is a Scala BDD testing framework.
  • Scala is a strongly-typed object-functional scalable language.
  • "Dependency injection vs. Cake pattern" (Cake Solutions Team Blog, December 15, 2011): Learn more about the cake pattern as an alternative to dependency injection, as it is implemented in several web application frameworks.

More from JavaWorld