This post originated from an RSS feed registered with Java Buzz
by Lalit Pant.
Original Post: Using JMock with Scala
Feed Title: All Things Runnable / Java
Feed URL: http://feeds.feedburner.com/lalitpant/java
Feed Description: Java related posts on Lalit Pant's Blog
While working on the Java version of Jiva earlier this year, I used JMock pretty heavily to test all of the non-deterministic code that is integral to the workings of a Genetic Algorithm.
When I ported Jiva to Scala, I did not really know if I could continue to use JMock for my testing needs. As it turned out, JMock worked out very well for testing Scala code.
I could go in and try to explain some of the ways in which JMock is used within Jiva, but I'm afraid that will introduce a lot of Genetic Algorithms related terminology, and will potentially divert attention from the main point of this post. So I will instead demonstrate the usage of JMock with Scala via a very simple (and contrived) example. If you want to see JMock in action within Jiva, you are welcome to browse the Subversion repository for the project: http://jiva-ng.googlecode.com/svn/trunk/src/test/scala/net/xofar/jiva/operators/
Back to the simple example...
Let's say we have a Class-under-test (Cut) in our code, and a Helper class that is used by Cut:
class Helper { def help = "helpfulResult" def helpWith(problem: String) = problem + " whatever help for String problem" def helpWith(problem: int) = problem + " whatever help for int problem" }
The Helper class has three different helper methods with different signatures. Cut also has three methods that delegate to the corresponding Helper methods. The idea here is that we want to test the Cut class by mocking out the helper used by Cut.
Let's jump right into the Test class (you will need to enable Java generics support in Scala for this to work; more on that later):
public class SExpectations extends Expectations { public <T> T withArg(Matcher<T> matcher) { return super.with(matcher); } }
SExpectations provides a method called withArg that just calls into the standard with method provided within its superclass by JMock. So essentially this class functions as an adapter that allows JMock parameter matching to be used by code written in Scala.
That's about it, actually. To reiterate: if you know how to use JMock (with Java), it should be pretty straightforward for you to start using it with Scala.
Some additional comments:
To work with JMock, you need to use Scala with the -Ygenerics and -target:jvm-1.5 options.
I used JUnit 3 for my Scala unit tests because the Scala Eclipse plugin does not quite support annotations. The standalone/command-line version of Scala supports annotations just fine, so it should be very possible to use JMock/JUnit 4 with Scala
I suppose I could have used MockObjectTestCase as a base class for my Tests. I just wanted to have more control over things as I worked through the Scala/JMock integration
I used the CGLib based Legacy ClassImposteriser because I needed to mock system classes for testing Jiva code