Posts in the “scala” category

Scala, MySQL, JDBC, database test code (for Google Cloud Run and Cloud SQL)

I was having a problem connecting from a Google Cloud Run service to a Cloud SQL instance, so as part of the process of troubleshooting the problem I created the following Scala + MySQL + JDBC database test code. The idea was to create the code as simply as possible — with as few dependencies as possible — to try to understand the problem.

Scala/Java: How to convert a stack trace to a string for printing with a logger

As a quick note, I just got a little bit better about logging stack traces when writing Java or Scala code. In Scala I used to get the text from a stack trace and then log it like this:

// this works, but it's not too useful/readable
logger.error(exception.getStackTrace.mkString("\n"))

In that code, getStackTrace returns a sequence, which I convert to a String before printing it.

Scala Cookbook: #1 new release in OOP and FP

Thanks to Scala being both an object-oriented programming language and a functional programming language, here on the last day of August, 2021, the Scala Cookbook (for Scala 3) is the #1 new release in both the OOP and FP categories.

UPDATE: The book is still #1 in both categories on September 7, 2021 — one month after the Kindle release.

(In the image shown, the Amazon OOP category is on the left, and the FP category is on the right.)

Scala Cookbook: A great geek gift idea

As this image shows, as of September 14, 2021, the Scala Cookbook is still a #1 new release in both the object-oriented programming (OOP) and functional programming (FP) categories. Because these are the two main computer programming categories, and the Cookbook has been a #1 new release for a month now, it was suggested that it might be a Great 2021 Geek Gift Idea. I can’t argue with that. :)

(The image here shows two browser windows, with the OOP page on the right overlapping the FP page on the left. The images were taken on September 14, 2021.)

Reading a File Into a Spark RDD (Scala Cookbook recipe)

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 20.2, Reading a File Into an Apache Spark RDD.

Problem

You want to start reading data files into a Spark RDD.

Solution

The canonical example for showing how to read a data file into an RDD is a “word count” application, so not to disappoint, this recipe shows how to read the text of the Gettysburg Address by Abraham Lincoln, and find out how many times each word in the text is used.

After starting the Spark shell, the first step in the process is to read a file named Gettysburg-Address.txt using the textFile method of the SparkContext variable sc that was introduced in the previous recipe:

Getting Started with Apache Spark (Scala Cookbook recipe)

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 20.1, Getting Started with Apache Spark.

Scala Problem

You’ve never used Spark before, and want to get started with it.

Solution

When you use Spark professionally, your data will probably be spread across multiple computers — maybe thousands of computers — but to get started with Spark you can do all of your work on one computer system in “local mode.” The easiest way to do this is to start the Spark shell, create an array, and go from there.

ScalaTest 103: Writing a first BDD test with ScalaTest

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:

ScalaTest 109: Mark your tests with tags to include or exclude them

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.

How to write Akka Actors: An example video game

Way back in 2013 — before my first fake heart attack followed by learning that I had thyroid cancer — I thought I was about to go “back to work”, and I decided to try to write another visual demo of Akka Actors before I went back to work. I gave myself 10 hours to write something, and at first I decided to just create some bubbles that would move about randomly on screen. But I got that working so fast that I decided to do something else.

Eventually I came up with the idea of a little “kill the bubbles” game, which turned into a “kill the characters” game. This video shows how it works:

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:

How to compare floating-point numbers in Scala

Scala FAQ: I need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be; how do I comparison floating-point numbers?

Solution

As in Java and many other languages, you solve this problem by creating a method that lets you specify the precision for your comparison. The following Scala “approximately equals” method demonstrates the approach:

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.

ScalaTest 108: How to test expected exceptions with ‘intercept’

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: