Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

The idea of using Scala to build unit tests for Java code has been thrown around. People are making the claim that Unit tests would be less effort to write because there's less boilerplate. In addition Scala, provides other "features" to provide "better" tests. Has anyone gone through this evaluation, and can you explain the pros/cons of using Scala for Java unit testing?

share|improve this question
add comment

1 Answer 1

I don't have a direct answer to your question, but perhaps you can infer some things about Scala-based testing from my experience with Clojure -- another JVM-based language -- for testing against Java APIs

I definitely believe some languages are better for writing tests than others. A lot of testing is about trying many variations of the same operation. My experience is that the number of keystrokes necessary to express test variations is smaller in Clojure than in Java. As you said, there's less boilerplate.

Since Clojure has a REPL, I can usually edit/run/debug tests without having to recompile.

There are downsides:

  • Our developers aren't interested in learning Clojure, so if a test breaks due to an API change, the onus is on me to fix it.
  • Our developers are less likely to run my tests because if a test fails, they won't necessarily be able to read the code to figure out what the test was trying to do.
  • There are many more people with Java experience than Clojure experience, so when I recruit our next test automation person, I'll have to spend more effort on recruiting and/or training.
  • When I leave the company someday, the odds are lower that my tests will still be useful because the Clojure expert will have left the building.

All of these are impediments. Whether they are compelling enough to avoid coding tests in something other than the product's native language depends on the culture and expertise in your organization.

share|improve this answer
1  
In regards to your third point, I believe all valid Java is valid Scala. There might be a couple exceptions/implementation errors, but barring that I'm pretty sure it's a complete superset. That's not to say that your point isn't a valid one to consider (especially for Java-->Clojure) but it might not be as applicable for Scala. –  corsiKa Jun 8 '11 at 17:21
    
Java syntax is a subset of Scala syntax? Really? I didn't know that. –  user246 Jun 8 '11 at 18:10
    
I could be mistaken, but I'm pretty sure it is. I know it is for Groovy (or at least, it's supposed to be - some minor inconsistencies exist.) Of course, I'm sure it comes with its own quirks - knowing syntax and knowing how to use it effective are two different things, and you will probably still experience some of the "new language jitters" when you bring people on board for Scala, regardless of if I am correct about it being a superset. –  corsiKa Jun 8 '11 at 18:50
    
They are compatible at the runtime level but as far as I can tell the Scala syntax is not a superset of Java. If you Google around for "scala java syntax superset", you will find claims that you can write scala code that looks like java code, but I think they mean "I reminiscent of" rather than "is exactly the same syntactically". So you could hire a developer who could write java-like scala code, but they wouldn't necessarily be able to read non-java-like scala code. –  user246 Jun 8 '11 at 18:56
    
That said, syntactically scala is more like java than clojure is like java, which is probably your main point. Sorry if my previous comment sounded pedantic. –  user246 Jun 8 '11 at 19:00
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.