https://github.com/bdmendes/smockito
Tiny Scala facade for Mockito.
https://github.com/bdmendes/smockito
facade mocking mockito scala testing wrapper
Last synced: 5 days ago
JSON representation
Tiny Scala facade for Mockito.
- Host: GitHub
- URL: https://github.com/bdmendes/smockito
- Owner: bdmendes
- License: mit
- Created: 2025-06-29T13:30:57.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2026-06-12T14:31:02.000Z (11 days ago)
- Last Synced: 2026-06-12T16:19:47.412Z (11 days ago)
- Topics: facade, mocking, mockito, scala, testing, wrapper
- Language: Scala
- Homepage: http://smockito.bdmendes.com/
- Size: 3.18 MB
- Stars: 31
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Smockito

[](https://github.com/bdmendes/smockito/actions)
[](https://app.codecov.io/gh/bdmendes/smockito)
[](https://central.sonatype.com/artifact/com.bdmendes/smockito_3/overview)
Smockito is a tiny framework-agnostic Scala 3 facade for [Mockito](https://github.com/mockito/mockito). It enables setting up unique method and value stubs for any type in a type-safe manner, while providing an expressive interface for inspecting received arguments and call counts.
Head to the [microsite](https://smockito.bdmendes.com) 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`:
```scala
libraryDependencies += "com.bdmendes" %% "smockito" % "" % 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](https://github.com/sbt/sbt-javaagent), you can simply add to your `build.sbt`:
```scala
javaAgents += "com.bdmendes" % "smockito_3" % "" % Test
```
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](https://docs.scala-lang.org/scala3/book/fun-eta-expansion.html) method reference, that you may easily express with `it`, and a [partial function](https://docs.scala-lang.org/scala3/book/fun-partial-functions.html) to handle the relevant inputs.
```scala
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.
```scala
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.
```scala
assert(repository.calls(it.getWith) == List(("john", "doe")))
assert(repository.times(it.getWith) == 1)
```