alvin's blog

12 ScalaTest tutorials

As I’ve written before, when I finished writing the Scala Cookbook it ended up being about 140 pages longer than my editor wanted, and I had to cut some content from the book. Unfortunately the chapter on “Logging & Testing” was one of the victims of the cut, but I’m glad to say that I’ve finally taken the time to convert that material to HTML. As a result, here are links to the 12 ScalaTest tutorials in that chapter:

The last one million visitors

I probably spend about 10 hours a year looking at data related to website visitors, and today was one of those days where I gave it 15 minutes of time. Here’s a quick look at the data.

This first image shows what browsers the visitors are using:

I write mostly about Open Source and Macs, so if IE is a little lower than usual, it may be because of that.

This image shows the number of people using desktop, mobile, and tablet clients:

How to use JUnit with Scala

Problem: You want to test your Scala code using JUnit.

Solution

Include the JUnit library in your project, and use it in the same way you’ve used it in Java projects, with a few minor changes.

Assuming you’re using SBT on your project, include JUnit into the project by adding this dependency line to your build.sbt file:

Scala-Style Logging with Grizzled-SLF4J

Problem: You want to add logging to an application in a more Scala-specific way than simply using SLF4J.

Solution

Grizzled-SLF4J is a thin wrapper around SLF4J that gives you logging in a more Scala-like way.

To get started with Grizzled-SLF4J, create a simple SBT project, then edit your build.sbt file so it has the dependencies you’ll need:

ScalaTest 112 - How to run ScalaTest unit tests in Eclipse

Problem: How do I use ScalaTest in Eclipse (or, How do I run my ScalaTest unit tests from Eclipse?)

Solution

I do a lot of work from the command line with Ant builds and similar things, but there are times I like to do things through Eclipse. Today I wanted to run my ScalaTest unit tests in Eclipse, and found this to be a straightforward task.

Besides Scala, Eclipse, and an Eclipse project, you'll need:

ScalaTest 111 - How to use Mock objects with ScalaTest

Problem: You want to use a mock object framework in your ScalaTest tests, such as Mockito.

Solution

ScalaTest offers support for the following mock testing frameworks:

  • ScalaMock
  • EasyMock
  • JMock
  • Mockito

Because the support for each framework is similar, let’s take a look at using Mockito.

Before starting, imagine that you have a login web service for your application, and rather than call the real web service during your tests, you just want to mock one up.

Syndicate content