If you are confused with a variety of test libraries in Scala ecosystem like me, then this is article for you.

In this article, I’d like to do a quick tour among Scala frameworks and libs and show capability of each of them on one same example to be able to compare approaches and how final test will look at very high level.

IMPORTANT NOTES:

  • This article doesn’t cover tests for particular technologies like akka, Play Framework, cats-effect - only general-purpose unit testing technologies - but they will be mentioned in the end in a short wrap-up table.
  • Perhaps not…

TL; DR

Shapeless coproduct is much better option for error modelling in Either[L, R] or EitherT[F, L, R] then regular sealed /sum structures because unlike them:

  • guarantees handling of all type of errors — no match is not exhastive warning, only compilation errors;
  • better errors composition;

Introduction

Using exception in Scala for error handling often considered as not very good engineering practise, in most of the cases, because it’s not type-safe: when you work with Try or some effect F[_] and want handle Throwable threw from downstream invocations, you never sure that you handling all possible erros because Throwable is not sealed structure…


Introduction

This blogpost inspired by StackOverflow answer for question: Compose Futures with Recovery in Scala: is it possible to compose Future's in a manner if one of them will fail, previous Future's execution result will be
rolled back via some function.

The problem

Case with Future can be generalized to some effect F[_], so we can say: How we can compose effects F[_] in a manner if one of them will fail, previous F[_] execution result will be rolled back via some function. This behaviour similar to what we know as Transaction.

Solution

For code solution below cats-effect version `2.3.1`…

Ivan Kurchenko

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store