Skip to main content

Extras - Testing Tools Core


StubTools.missing is a tool for a stub (a simple function for testing) so that you don't need to use mock frameworks.


In most cases, you probably don't want to use StubTools.missing directly. It is recommended to use StubToolsCats or StubToolsFx instead.


import cats.syntax.all._
import eu.timepit.refined.types.all._
import eu.timepit.refined.cats._
import io.estatico.newtype.macros.newtype
import extras.testing.StubTools

object types {
@newtype case class Id(value: PosInt)
@newtype case class Name(value: NonEmptyString)
import types._

trait MyService {
def findName(id: Id): Option[Name]

object MyServiceStub {
def apply(f: => Option[Id => Option[Name]]): MyService = new MyService {
override def findName(id: Id): Option[Name] = f.fold[Option[Name]](throw StubTools.missing)(_(id))

class Hello(myService: MyService) {
def hello(id: Id): String = {
.fold(s"No name found for id ${id.value}") { name =>
s"Hello ${name.value}"

val expectedId = Id(1)
// expectedId: Id = 1
val expectedName = Name("Kevin")
// expectedName: Name = Kevin

val myService: MyService = MyServiceStub(((id: Id) =>
if (id.value === expectedId.value)
// myService: MyService = repl.MdocSession$MdocApp0$MyServiceStub$$anon$1@378b9449
val hello = new Hello(myService)
// hello: Hello = repl.MdocSession$MdocApp0$Hello@1a4fa03d
// Hello Kevin
// No name found for id 2
/* If you don't expect Hello to use MyService.findName,
* you can simply remove feeding the function for that operation
* and StubTools let you know where it fails if Hello uses MySerivce.findName.
val myService2: MyService = MyServiceStub(none)
val hello2 = new Hello(myService2)
// extras.testing.StubTools$MissingStubException:
// >> Missing Stub implementation at
// >> repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$findName$1.apply(
// >> ---
// >> Details:
// >> repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$findName$1.apply(
// at repl.MdocSession$MdocApp0$MyServiceStub$$anon$1$$anonfun$findName$1.apply(
// at scala.Option.fold(Option.scala:263)
// at repl.MdocSession$MdocApp0$MyServiceStub$$anon$1.findName(
// at repl.MdocSession$MdocApp0$Hello.hello(
// at repl.MdocSession$MdocApp0$$anonfun$2.apply$mcV$sp(
// at repl.MdocSession$MdocApp0$$anonfun$2.apply(
// at repl.MdocSession$MdocApp0$$anonfun$2.apply(
// at mdoc.internal.document.DocumentBuilder$$doc$.crash(DocumentBuilder.scala:75)
// at repl.MdocSession$MdocApp0$.<clinit>(
// at repl.MdocSession$MdocApp.<init>(
// at repl.MdocSession$.app(
// at mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$2(DocumentBuilder.scala:89)
// at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
// at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
// at scala.Console$.withErr(Console.scala:193)
// at mdoc.internal.document.DocumentBuilder$$doc$.$anonfun$build$1(DocumentBuilder.scala:89)
// at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
// at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
// at scala.Console$.withOut(Console.scala:164)
// at mdoc.internal.document.DocumentBuilder$$doc$.build(DocumentBuilder.scala:88)
// at mdoc.internal.markdown.MarkdownBuilder$.$anonfun$buildDocument$2(MarkdownBuilder.scala:48)
// at mdoc.internal.markdown.MarkdownBuilder$$anon$

Why not just use mock framework for convenience? To answer that, please read Pitfalls of Mocking in tests from Xebia Functional (formerly known as 47 Degrees)

Besides what the blog tells you, mock frameworks often make you do bad practice like testing the implementation details with verify.

There is also an issue when your mock is not correctly set. You may get a NullPointerException for that, but it doesn't tell you where it's from and why you get it.