Skip to main content

Scala and Testing

In our company, we have a rich and mature Java stack that we draw from for nearly every project. However, I politely told my manager that, for this project, we would be using Scala. I explained that we would still be able to use everything on the Stack, and we would get it done faster and cheaper that way.

So, happily, he gave me a little bit of line.

When it came to writing Unit Tests, we wanted to use EasyMock, an awesome Java library for mocking, stubbing, etc.

Everything worked great, but I ran into one little problem:

If I have the following code:


EasyMock.expect(myClass.myMethod(myParam)).andReturn(myReturn);
EasyMock.replay(myClass);
... do unit testing ...
EasyMock.verify(myClass);
EasyMock.reset(myClass);


Then, I get on the replay call:


java.lang.IllegalArgumentException: not a proxy instance
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:637)
at org.easymock.EasyMock.getControl(EasyMock.java:1600)
at org.easymock.EasyMock.replay(EasyMock.java:1502)...


This is because I am not using the classextension.EasyMock class. However, if I use that, then Scala complains that "expect" isn't a member of classextension.EasyMock.

So, here is the solution that I came up with:

I use import shortnames to include both EasyMock classes:


import org.easymock.{EasyMock=>EM};
import org.easymock.classextension.{EasyMock=>CEM};


And then in the code, I do the following:


EM expect(myClass myMethod myParam) andReturn myReturn
CEM replay myClass
...unit test stuff...
CEM verify myClass
CEM reset myClass


And it works great!

Has anyone else found a good solution for this?

The next problem that I have run into is method matching when one of the values passed in is an array projection.

Observe the following code:


myClass.myMethod(param1, param2, str split "_" drop 1)


The last parameter returns type Array.Projection. If I have the exact same code in my expect, then this particular parameter is still listed as being unequal.

The only way that I have found so far around this is:


EM expect(myClass.myMethod(EM anyObject[type1], EM anyObject[type2], EM anyObject[type3])) andReturn myReturn


Not a big fan of this strategy, but it works. Any ideas?

Comments

Popular posts from this blog

How Many Teeth Does The Tooth Fairy Pick Up Each Night in Utah?

Somebody asked me a question about my Tooth Fairy post the other day that got me thinking. How many baby teeth are lost every day in Utah?

I began with Googling. Surely someone else has thought of this and run some numbers, right? Lo, there is a tooth fairy site that claims that the Tooth Fairy collects 300,000 teeth per night.

That's a lot; however, when I ran the numbers, it started to seem awfully low.

Let's assume that the Tooth Fairy collects all baby teeth regardless of quality and we assume that all children lose all their baby teeth. The world population of children sits at 2.2 billion, with 74.2 million of them in the United States. Of those, approximately 896,961 of them are in Utah. This means that somewhere around .04077% of the world's children are in Utah.

If we assume that kids in Utah lose teeth at the same rate as all other children in the world and that each day in the year is just as likely as the rest to lose a tooth, then we have that of the alleged …

Twas the Night Before Pi Day

Twas the Night Before Pi Day
by Joshua Cummings

Twas the night before Pi Day, when Archimedes, the muse,
Went to pay me a visit whilst I took a snooze.

I'd visions of carrot cake, candy, and cheese
When dashed open my window and entered a breeze
That stirred me to consciousness, albeit in time
To see my face plastered in pie of key lime.

And once I'd removed the fruit from my eyes
And put on my spectacles did I realize
That before me presented a most divine spectre
Who clearly possessed the Key Lime Projector.

"It's a fulcrum, you see!" he began to explain,
"All I use is this crank to cause the right strain,
"Then releasing its fetter it launches sky high
"The juiciest pie of key lime in your eye!"

I sat there immobile for what seemed a year,
As the spectre protested I his genius revere,
When clearly it came, the fine revelation,
Of his piety, honor, achievements, and station.
With his little old catapult, so lively and quick,
I knew in a moment that this must be …

I don't know you from Adam OR How to Tie Yourself Back to Adam in 150 Easy Steps

Last Sunday, I was working on my genealogy on familysearch.org, a free site provided by The Church of Jesus Christ of Latter-Day Saints for doing pretty extensive family history. While looking for information about a Thomas Neal, I found an individual who had done a bunch of work on his family including is tie into the Garland family, which tied in through Thomas's wife.

So, while I was pondering what to do about Thomas Neal (who's parents I still haven't found), I clicked up the Garland line. It was pretty cool because it went really far back; it's always fun to see that there were real people who you are really related to back in the 14th century or what not.
As I worked my way back through the tree, I noticed it dead-ended at Sir Thomas Morieux, who, according to the chart, was the maternal grandfather-in-law of Humphy Garland (b. 1376).  The name sounded pretty official, so I thought I'd Google him. I learned from Wikipedia that Sir Thomas Morieux married Blanc…