Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Scala to unit test Java?

I'm familiar with using mock objects to help unit test my Java types, but find the inflexibility can lead to verbose and cumbersome test types and a lot of repetition.

I've looked at using Groovy for unit tests with moderate success. I'm interested in learning Scala for itself, but would also like some advice on using it for testing Java. Does anyone have any advice or references on using Scala to do this?

Update to clarify what I'm after:

I've seen that there exists tools for unit testing in Scala, like ScalaTest. and ScalaCheck, but they focus on how to do the mechanics of testing. I'm looking for some best-practices and/or hints and tips for how unit testing should be approached with Scala. For example an equivalent of the Javaworld JUnit best practices article or a list of anti-patterns to point out common pitfalls or useful conventions.

like image 207
Rich Seller Avatar asked Jul 18 '09 22:07

Rich Seller


People also ask

Does JUnit work with Scala?

To run a test suite using JUnit in Scala IDE, open the source file of the test, right-click in it, and select Run as → JUnit Test. You can re-run your test using the launch configuration which is automatically created.

What is FunSuite in Scala?

FunSuite is a Scala testing library with the following goals: Reuse JUnit: FunSuite is implemented as a JUnit runner and tries to build on top of existing JUnit functionality where possible. Any tool that knows how to run a JUnit test suite knows how to run FunSuite, including IDEs like IntelliJ.


2 Answers

You can certainly use Scala to test Java. In fact, since the Scala testing frameworks integrate well with JUnit, you can write the tests in Scala, and run them from Eclipse through JUnit.

There are three important frameworks: ScalaTest, general, very flexible; ScalaCheck, based on a Haskell testing framework, it generate random data to test conditions for you; Specs, general, similar in scope to ScalaTest, but has a more BDD-oriented syntax.

like image 81
Daniel C. Sobral Avatar answered Oct 12 '22 21:10

Daniel C. Sobral


This presentation has some hints on using Scala for testing Java code: sneaking Scala into your organization.

like image 26
Jus12 Avatar answered Oct 12 '22 21:10

Jus12