Skip to content
bdmendesPublic

About

Tiny Scala facade for Mockito.

Topics

Resources

Stars

32 stars

Watchers

2 watching

Forks

Latest commit

 

History

958 Commits

Folders and files

Repository files navigation

Smockito

Build Codecov Maven Central

Smockito is a tiny framework-agnostic Scala 3 facade for Mockito. It enables setting up unique method and value stubs for any reference type in a type-safe manner, while providing an expressive interface for inspecting received arguments and call counts.

Head to the microsite for the full documentation and API reference.

Quick Start

To use Smockito in an existing sbt project with Scala 3, add the following dependency to your build.sbt:

libraryDependencies += "com.bdmendes" %% "smockito" % "<version>" % Test

Do not depend on Mockito directly.

If targeting Java 24+, you need to add the Smockito JAR as a Java agent to enable the runtime bytecode manipulation that Mockito depends on. If you use the sbt-javaagent plugin, you can simply add to your build.sbt:

javaAgents += "com.bdmendes" % "smockito_3" % "<version>" % Test

If using sbt 2+, depending on your test configuration, some runtime dependencies may not be available in the classpath. In that case, resort to the system loader:

Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Raw

In your specification, extend Smockito. This will bring the mock method and relevant conversions to scope. To set up a mock, add stub definitions with the on method, which requires an eta-expanded method reference, that you may easily express with it, and a partial function to handle the relevant inputs.

import com.bdmendes.smockito.Smockito

abstract class Repository[T](val name: String):
  def getWith(startsWith: String, endsWith: String): List[T]

case class User(username: String)

class RepositorySpecification extends Smockito:
  val repository = mock[Repository[User]]
    .on(it.getWith):
      case ("john", name) if name.nonEmpty => List(User("johndoe"))

A Mock[T] is a T both at compile time and runtime.

  assert(repository.getWith("john", "doe") == List(User("johndoe")))

You may reason about method interactions with calls and times. If arguments are not needed, times is more efficient.

  assert(repository.calls(it.getWith) == List(("john", "doe")))
  assert(repository.times(it.getWith) == 1)

Arguments, as required by stubs and yielded by verifications, are of the most convenient shape to the caller: a scalar for single argument methods and a named tuple for multi-argument methods.

  assert(repository.calls(it.getWith).map(_.startsWith) == List("john"))

About

Tiny Scala facade for Mockito.

Topics

Resources

Stars

32 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages