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 …

Mental Math Tricks: Is this divisible by 17?

Image via WikipediaSo, most know how to tell if something is divisible by 2 or 5, and many know how to tell if something is divisible by 9. What about other numbers?

So, here are strategies for discovering divisibility from 2 to 10, and then we'll talk about some rarer, more surprising divisibility tricks:

Divisible by two:If the number ends in 0, 2, 4, 6, or 8, it is divisible by 2.

Divisible by five:If the number ends in 0 or 5, it is divisible by 5.

Divisible by ten:If the number ends in 0, it is divisible by 10.

Divisible by nine: If you add all the digits in a number together and that new number is divisible by 9, then it is also divisible by 9.

Example #1: 189 -> 1 + 8 + 9 = 18, 18 is divisible by 9, so 189 is also divisible by 9
Example #2: 137781 -> 1 + 3 + 7 + 7 + 8 + 1 = 27, 27 is divisible by 9, so 137781 is also divisible by 9
(Note: If adding six numbers together in your head seems difficult, look for my next post on number-grouping tricks. Soon, adding six numbe…

BYU and the Sunday Compromise?

I read an article by Brad Rock this morning where he quoted heavily from Dr. Thomas Forsthoefel who was giving his opinion on religious institutions being involved in sports. BYU, of course, came up.
I think Forsthoefel came off sounding a bit misinformed about the culture, drive, mission, etc. of BYU. Below is the email that I sent to Brad Rock this morning after finishing the article:
Brad -
That was an interesting article. I tend to disagree with Forsthoefel, though, or at least disagree with what I may have read into his comments.
A quote in your article says:
"There may be a kind of growing pain. BYU is in the real world and the real world works on Sunday. Can we (BYU) live with the adjustment? I'm empathetic with that, whatever decision is made, people are going to be unhappy.… Some will say get with the program, we'll be OK at the next level, others will say we've sold out and we've made a deal with the world."

This seems to suggest one or two things; fir…