Scala, Java, Unix, MacOS tutorials (page 293)

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:

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:

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:

Problem: You want to add Java-style logging to your Scala application, and you’re comfortable with the Java SLF4J library.

Solution

Assuming you’re using SBT, include the necessary SLF4J dependencies in your build.sbt file:

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.

Problem: When using ScalaTest, you want to temporarily disable one or more tests, presumably until you can get them working again.

Solution

When using BDD-style tests, change it method calls to ignore:

Problem: You want a way to label your individual ScalaTest unit tests, so you can easily choose to include or exclude them when running your test suite. For instance, you may want to tag long-running tests like database or web service tests, because they take a long time to run, and you don’t want to run them all the time.

Solution

Create one or more custom tags, then include those tags in your test specifications. When you run your tests, declare which tests you want to run, or not run.

Problem: Using ScalaTest, you want to test a portion of your code that should throw an exception under certain conditions.

Solution

Use the intercept method to verify the exception occurs. In the following example, the boom method will always throw an exception. The intercept method lets you catch that exception, so you can verify that this portion of the method works as desired:

Problem: You want to note that a ScalaTest unit test needs to be created, but you’re not ready to write it yet.

Solution

Instead of supplying the body of a test method, mark the test as pending. In the ScalaTest TDD style, create a pending test like this:

Problem: You’d like to have better output from your ScalaTest assert tests, output that shows the expected and actual values.

Solution

One approach is to use the === method instead of ==. When an assert test fails, the === method output shows the two values from the test.

Problem: You want to add more unit tests and a main test suite to your ScalaTest tests.

Solution

To add more unit tests to your project, just create new test classes. For instance, to add a set of TDD-style tests for the Topping class, just create a ToppingTests class in the src/test/scala/com/acme/pizza directory:

Problem: You want to make your ScalaTest unit tests more BDD-like by adding “Given/When/Then” behavior to them.

Solution

Mix the GivenWhenThen trait into your FunSpec BDD test, then add the Given/When/Then conditions, as shown in the following code:

Problem: You want to write your ScalaTest tests using a behavior-driven development (BDD) style.

Solution

Extend the ScalaTest FunSpec trait, typically with the BeforeAndAfter trait. Then use the approach shown in the following PizzaSpec test class.

A series of tests begins with the describe method, with individual tests declared in it methods:

Problem: You want to use test-driven development (TDD) style tests in your Scala applications, and need to see examples of how to write them.

Solution

Have your test classes extend the ScalaTest FunSuite, and then write your tests. Because most tests involve a setup and teardown process, you’ll usually also want to add in the BeforeAndAfter trait as well.

Problem: You want to begin using ScalaTest to write TDD- or BDD-style unit tests for your Scala applications.

I missed this while I was traveling, but the programming language I wrote about, Scala, was named a “2014 Technology of The Year”.

Linux “open files” FAQ: Can you share some examples of how to show “open files” on a Linux system (i.e., how to use the lsof command)?

The Linux lsof command lists information about files that are open by processes running on the system. (The lsof command itself stands for “list of open files.”) In this brief article I’ll just share some lsof command examples. If you have any questions, just let me know.

Linux processes FAQ: Is there a utility to show Linux processes interactively, like the Unix/Linux ps command, but more like a GUI ro character-based interactive tool?

Per the Github page, “scoverage is a free Apache licensed code coverage tool for scala that offers statement and branch coverage. scoverage is available for both maven and sbt. scoverage was originally based on SCCT.”