Skip to content

Value class support #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
style = defaultWithAlign

docstrings = JavaDoc
maxColumn = 120
rewrite.rules = [RedundantBraces, RedundantParens, SortImports]
spaces.inImportCurlyBraces = true
indentOperator = spray
unindentTopLevelOperators = true
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
language: java

jdk:
- oraclejdk8
- oraclejdk8

#Skipping install step to avoid having Travis run arbitrary './gradlew assemble' task
# https://docs.travis-ci.com/user/customizing-the-build/#Skipping-the-Installation-Step
install:
- true
- true

#Don't build tags
branches:
Expand All @@ -18,4 +18,4 @@ branches:

#Build and perform release (if needed)
script:
- ./gradlew build -s && ./gradlew ciPerformRelease
- ./gradlew build -s && ./gradlew ciPerformRelease
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,22 @@ This trait exposes all the existent `org.mockito.ArgumentMatchers` but again it
* `eq` was renamed to `eqTo` to avoid clashing with the Scala `eq` operator for identity equality
* `any` resolves to the correct type most of the times, removing the need of using the likes of `anyString`, `anyInt`, etc
* `isNull` and `isNotNull` are deprecated as using nulls in Scala is clear code smell
* Adds support for value classes via `anyVal[T]` and `eqToVal[T]()`

Again, the companion object also extends the trait to allow the usage of the API without mixing-in the trait in case that's desired

### Value Class Matchers

The matchers for the value classes always require the type to be explicit, apart from that, they should be used as any other matcher, e.g.
```
when(myObj.myMethod(any[MyValueClass]) thenReturn "something"

myObj.myMethod(MyValueClass(456)) shouldBe "something"

verify(myObj).myMethod(eqToVal[MyValueClass](456))
```


## Authors

* **Bruno Bonanno** - *Initial work* - [bbonanno](https://github.com/bbonanno)
Expand Down
86 changes: 63 additions & 23 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
import sbt.Keys._

import scala.io.Source
import scala.language.postfixOps
import scala.util.Try

name := "mockito-scala"
organization := "org.mockito"

licenses := Seq("MIT" -> url("https://opensource.org/licenses/MIT"))
homepage := Some(url("https://github.com/mockito/mockito-scala"))
scmInfo := Some(ScmInfo(url("https://github.com/mockito/mockito-scala"), "[email protected]:mockito/mockito-scala.git"))
developers := List(
Developer("bbonanno", "Bruno Bonanno", "[email protected]", url("https://github.com/bbonanno"))
)
val _scalaVersion = "2.12.6"

//Load version from the file so that Gradle/Shipkit and SBT use the same version
version := {
val pattern = """^version=(.+)$""".r
val source = Source.fromFile("version.properties")
val version = Try(source.getLines.collectFirst {
case pattern(v) => v
}.get)
source.close
version.get
}
lazy val commonSettings =
Seq(
scalaVersion := _scalaVersion,
organization := "org.mockito",
//Load version from the file so that Gradle/Shipkit and SBT use the same version
version := {
val pattern = """^version=(.+)$""".r
val source = Source.fromFile("version.properties")
val version = Try(source.getLines.collectFirst {
case pattern(v) => v
}.get)
source.close
version.get
}
)

scalaVersion := "2.12.6"
lazy val commonLibraries = Seq(
"org.mockito" % "mockito-core" % "2.19.0",
"org.scala-lang" % "scala-reflect" % _scalaVersion
)

libraryDependencies += "org.mockito" % "mockito-core" % "2.19.0"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.12.6"
lazy val core = (project in file("core"))
.dependsOn(macroSub % "compile-internal, test-internal")
.settings(
commonSettings,
name := "mockito-scala",
libraryDependencies ++= commonLibraries,
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.5" % Test
),
// include the macro classes and resources in the main jar
mappings in (Compile, packageBin) ++= mappings
.in(macroSub, Compile, packageBin)
.value,
// include the macro sources in the main source jar
mappings in (Compile, packageSrc) ++= mappings
.in(macroSub, Compile, packageSrc)
.value,
licenses := Seq("MIT" -> url("https://opensource.org/licenses/MIT")),
homepage := Some(url("https://github.com/mockito/mockito-scala")),
scmInfo := Some(
ScmInfo(
url("https://github.com/mockito/mockito-scala"),
"[email protected]:mockito/mockito-scala.git"
)
),
developers := List(
Developer(
"bbonanno",
"Bruno Bonanno",
"[email protected]",
url("https://github.com/bbonanno")
)
)
)

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
lazy val macroSub = (project in file("macro"))
.settings(
commonSettings,
libraryDependencies ++= commonLibraries,
publish := {},
publishLocal := {}
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class MockitoEnhancerUtil {

static <T> T stubMock(T aMock) {
Stream.of(aMock.getClass().getMethods())
.filter(m -> !isStatic(m.getModifiers()))
.filter(m -> m.getName().contains("$default$"))
.forEach(defaultParamMethod -> when(call(defaultParamMethod, aMock)).thenCallRealMethod());
.filter(m -> !isStatic(m.getModifiers()))
.filter(m -> m.getName().contains("$default$"))
.forEach(defaultParamMethod -> when(call(defaultParamMethod, aMock)).thenCallRealMethod());

return aMock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ package org.mockito
import org.mockito.matchers._

/**
* Trait that provides some syntax sugar and type mapping.
*
* It mostly forwards the calls to org.mockito.ArgumentMatchers, but with a few improvements to make it more scala-like
* It also renames the "eq" matcher to "eqTo" as in Scala "eq" is a keyword used to do object identity equality
*
* @author Bruno Bonanno
*
*/
trait ArgumentMatchersSugar extends AnyMatchers with EqMatchers with ThatMatchers with StringThatMatchers with NullMatchers
* Trait that provides some syntax sugar and type mapping.
*
* It mostly forwards the calls to org.mockito.ArgumentMatchers, but with a few improvements to make it more scala-like
* It also renames the "eq" matcher to "eqTo" as in Scala "eq" is a keyword used to do object identity equality
*
* @author Bruno Bonanno
*
*/
trait ArgumentMatchersSugar
extends AnyMatchers
with EqMatchers
with ThatMatchers
with StringThatMatchers
with NullMatchers

/**
* Simple object to allow the usage of the trait without mixing it in
*
* @author Bruno Bonanno
*
*/
* Simple object to allow the usage of the trait without mixing it in
*
* @author Bruno Bonanno
*
*/
object ArgumentMatchersSugar extends ArgumentMatchersSugar
Loading