Chapter 16
Unit Testing

Your code always does what you type—unit tests will assert that it does what you actually meant. As you develop your application, unit testing further helps ensure your code continues to meet your expectations.

Learning to write unit tests in Scala will benefit you in a number of ways:

  • It’s a nice way to introduce Scala on your current projects. Even though your production code may be in Java, you can write the test code in Scala.

  • It’s a good way to learn Scala itself. As you learn the language, you can experiment with the language and its API by writing test cases.

  • It improves your design. It’s very hard to unit test large and complex code. In order to test it, you’d end up making the code smaller. This will lead to a better design by making the code more cohesive, loosely coupled, easier to understand, and easier to maintain.

You have a few options for unit testing in Scala; you can use Java-based tools like JUnit or use ScalaTest. ScalaTest has changed quite a bit in recent years—when tools evolve quickly, any attempt to provide a detailed example will run the risk of the code becoming obsolete very soon. Thus, this chapter provides a short introduction and some recommendations. Refer to the tutorials[6] for an extensive coverage of ScalaTest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.107.220