Skip to content

Make tests scala 3 friendlier #576

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

Draft
wants to merge 1 commit into
base: release/2.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.mockito.cats

import cats.implicits._
import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.mockito.cats
import cats.Eq
import cats.implicits._
import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }

Expand Down
4 changes: 2 additions & 2 deletions cats/src/test/scala/org/mockito/cats/MockitoCatsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.Eq
import cats.data.{ EitherT, OptionT }
import cats.implicits._
import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, MockitoSugar }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }

Expand Down Expand Up @@ -251,7 +251,7 @@ class MockitoCatsTest extends AnyWordSpec with Matchers with MockitoSugar with A

doAnswerFG[Future, ErrorOr, ValueClass](ValueClass("mocked!")).when(aMock).returnsFutureEither("hello")
doAnswerFG[Future, ErrorOr, String, ValueClass]((s: String) => ValueClass(s + " mocked!")).when(aMock).returnsFutureEither("hi")
doAnswerFG[Future, ErrorOr, InvocationOnMock, ValueClass] { i: InvocationOnMock => ValueClass(i.arg[String](0) + " invocation mocked!") }
doAnswerFG[Future, ErrorOr, InvocationOnMock, ValueClass]((i: InvocationOnMock) => ValueClass(i.arg[String](0) + " invocation mocked!"))
.when(aMock)
.returnsFutureEither("hola")
doAnswerFG[Future, Option, Int, Boolean, String]((i: Int, b: Boolean) => s"$i, $b").when(aMock).returnsFutureOptionFrom(42, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package user.org.mockito
import java.util.concurrent.atomic.AtomicInteger

import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, MockitoSugar }
import org.mockito._
import user.org.mockito.matchers.{ ValueCaseClassInt, ValueCaseClassString, ValueClass }
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package user.org.mockito
import java.util.concurrent.atomic.AtomicInteger

import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, IdiomaticStubbing }
import org.mockito._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import user.org.mockito.matchers.{ ValueCaseClassInt, ValueCaseClassString, ValueClass }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class MalformedClassError extends AnyWordSpecLike with Matchers with IdiomaticMo
object MalformedClassError {
sealed trait Permissions
object Permissions {
final case object Allowed extends Permissions
final case object Denied extends Permissions
case object Allowed extends Permissions
case object Denied extends Permissions
}
type Allowed = Permissions.Allowed.type

Expand Down
13 changes: 2 additions & 11 deletions scalatest/src/test/scala/user/org/mockito/MockitoSugarTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import org.mockito.exceptions.misusing.WrongTypeOfReturnValue
import org.mockito.exceptions.verification.{ ArgumentsAreDifferent, WantedButNotInvoked }
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.{ CallsRealMethods, DefaultAnswer, ScalaFirstStubbing }
import org.mockito.{ ArgumentMatchersSugar, MockitoSugar }
import org.mockito._
import org.scalactic.Prettifier
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.{ EitherValues, OptionValues }
import user.org.mockito.matchers.{ ValueCaseClassInt, ValueCaseClassString, ValueClass }
import user.org.mockito.model.JavaFoo

import scala.reflect.io.AbstractFile
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

Expand Down Expand Up @@ -317,14 +316,6 @@ class MockitoSugarTest extends AnyWordSpec with MockitoSugar with Matchers with
org.takesManyValueClasses(new ValueClass("1"), ValueCaseClassInt(2), ValueCaseClassString("3")) shouldBe "ValueClass(1)-ValueCaseClassInt(2)-ValueCaseClassString(3)"
}

"not mock final methods" in {
val abstractFile = mock[AbstractFile]

when(abstractFile.path) thenReturn "sammy.scala"

abstractFile.path shouldBe "sammy.scala"
}

"be serialisable" in {
val list = mock[java.util.List[String]](withSettings.name("list1").serializable())
when(list.get(eqTo(3))) thenAnswer "mocked"
Expand Down Expand Up @@ -353,7 +344,7 @@ class MockitoSugarTest extends AnyWordSpec with MockitoSugar with Matchers with

aMock.bar shouldBe "hola"

val ex = the[WrongTypeOfReturnValue] thrownBy (aMock.returnBar shouldBe "hola")
val ex = the[WrongTypeOfReturnValue] thrownBy aMock.returnBar
ex.getMessage should include("Default answer returned a result with the wrong type")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IdiomaticMockitoFixtureClassTest extends flatspec.FixtureAnyFlatSpec with
withFixture(test.toNoArgTest(theFixture))
}

"Mockito" should "verifyNoMoreInteractions fixture objects" in { f: FixtureParam =>
"Mockito" should "verifyNoMoreInteractions fixture objects" in { (f: FixtureParam) =>
"mocked" willBe returned by f.foo.bar("pepe")

f.foo wasNever called
Expand All @@ -32,7 +32,7 @@ class IdiomaticMockitoFixtureClassTest extends flatspec.FixtureAnyFlatSpec with
}
}

"Mockito" should "verify no calls on fixture objects methods" in { f: FixtureParam =>
"Mockito" should "verify no calls on fixture objects methods" in { (f: FixtureParam) =>
"mocked" willBe returned by f.foo.bar("pepe")
"mocked" willBe returned by f.foo.baz

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class IdiomaticMockitoWithExpectFixtureClassTest extends flatspec.FixtureAnyFlat
super.withFixture(test.toNoArgTest(theFixture))
}

"expect no calls TO" should "verify no calls on fixture objects methods" in { f: FixtureParam =>
"expect no calls TO" should "verify no calls on fixture objects methods" in { (f: FixtureParam) =>
"mocked" willBe returned by f.foo.bar("pepe")
"mocked" willBe returned by f.foo.baz

Expand All @@ -37,7 +37,7 @@ class IdiomaticMockitoWithExpectFixtureClassTest extends flatspec.FixtureAnyFlat
}
}

"expect no calls ON" should "verify no calls on a mock inside a fixture object" in { f: FixtureParam =>
"expect no calls ON" should "verify no calls on a mock inside a fixture object" in { (f: FixtureParam) =>
f.foo.bar("pepe") returns "mocked"

expect no calls on f.foo
Expand All @@ -49,7 +49,7 @@ class IdiomaticMockitoWithExpectFixtureClassTest extends flatspec.FixtureAnyFlat
}
}

it should "prevent usage of 'no calls to' when 'no calls on' is intended" in { f: FixtureParam =>
it should "prevent usage of 'no calls to' when 'no calls on' is intended" in { (f: FixtureParam) =>
the[MissingMethodInvocationException] thrownBy {
expect no calls to f.foo
} should have message
Expand All @@ -62,7 +62,7 @@ class IdiomaticMockitoWithExpectFixtureClassTest extends flatspec.FixtureAnyFlat
|""".stripMargin
}

"expect noMore calls on" should "verify no more calls on a mock inside a fixture object" in { f: FixtureParam =>
"expect noMore calls on" should "verify no more calls on a mock inside a fixture object" in { (f: FixtureParam) =>
f.foo.bar("pepe") returns "mocked"

f.foo.bar("pepe") shouldBe "mocked"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.mockito.scalaz

import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }
import _root_.scalaz._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.mockito.scalaz

import _root_.scalaz._
import Scalaz._
import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }
import scalaz.Scalaz._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ Future, Promise }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.mockito.scalaz
import _root_.scalaz._
import Scalaz._
import org.mockito.invocation.InvocationOnMock
import org.mockito.{ ArgumentMatchersSugar, MockitoSugar }
import org.mockito._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ EitherValues, OptionValues }

Expand Down Expand Up @@ -252,7 +252,7 @@ class MockitoScalazTest extends AnyWordSpec with Matchers with MockitoSugar with

doAnswerFG[Future, ErrorOr, ValueClass](ValueClass("mocked!")).when(aMock).returnsFutureEither("hello")
doAnswerFG[Future, ErrorOr, String, ValueClass]((s: String) => ValueClass(s + " mocked!")).when(aMock).returnsFutureEither("hi")
doAnswerFG[Future, ErrorOr, InvocationOnMock, ValueClass] { i: InvocationOnMock => ValueClass(i.arg[String](0) + " invocation mocked!") }
doAnswerFG[Future, ErrorOr, InvocationOnMock, ValueClass]((i: InvocationOnMock) => ValueClass(i.arg[String](0) + " invocation mocked!"))
.when(aMock)
.returnsFutureEither("hola")
doAnswerFG[Future, Option, Int, Boolean, String]((i: Int, b: Boolean) => s"$i, $b").when(aMock).returnsFutureOptionFrom(42, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.{ File, FileOutputStream, ObjectOutputStream }
import java.util
import org.hamcrest.core.IsNull
import org.mockito.IdiomaticMockitoBase.Times
import org.mockito.VerifyOrder
import org.mockito.{ VerifyInOrder, VerifyOrder }
import org.mockito.captor.ArgCaptor
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.DefaultAnswer
Expand Down Expand Up @@ -554,7 +554,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list1.get(1)

implicit val order = inOrder(list1)
implicit val order: VerifyInOrder = inOrder(list1)

val result = list1.get(1).was(called) andThen list1.get(0).was(called)

Expand All @@ -567,7 +567,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list1.get(1)

implicit val order = inOrder(list1)
implicit val order: VerifyInOrder = inOrder(list1)

var result: Result = success

Expand All @@ -593,8 +593,8 @@ The Mockito trait is reusable in other contexts

list1.get(0); list1.size; list1.get(0); list1.size

implicit val order = inOrder(list1)
val result = got {
implicit val order: VerifyInOrder = inOrder(list1)
val result = got {
list1.get(0) was called
list1.size() was called
list1.get(0) wasNever called
Expand All @@ -608,13 +608,13 @@ The Mockito trait is reusable in other contexts

def callbacks1 = {
val list = mock[java.util.List[String]]("list")
list.get(*) answers { i: Int => s"The parameter is ${i.toString}" }
list.get(*) answers { (i: Int) => s"The parameter is ${i.toString}" }
list.get(2) must_== "The parameter is 2"
}

def callbacks2 = {
val list = mock[java.util.List[String]]("list")
list.get(*) answers { i: Int => (i + 1).toString }
list.get(*) answers { (i: Int) => (i + 1).toString }
list.get(1) must_== "2"
list.get(5) must_== "6"
}
Expand Down
20 changes: 10 additions & 10 deletions specs2/src/test/scala/org/mockito/specs2/MockitoScalaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.mockito.specs2
import java.io.{ File, FileOutputStream, ObjectOutputStream }
import java.util
import org.hamcrest.core.IsNull
import org.mockito.VerifyOrder
import org.mockito.{ VerifyInOrder, VerifyOrder }
import org.mockito.captor.ArgCaptor
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.DefaultAnswer
Expand Down Expand Up @@ -525,7 +525,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list2.get(0)

implicit val order = inOrder(list1, list2)
implicit val order: VerifyInOrder = inOrder(list1, list2)
(there was one(list1).get(0) andThen
one(list2).get(0)).message must_== "The mock was called as expected"
}
Expand All @@ -541,7 +541,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list2.get(0)

implicit val order = inOrder(ignoreStubs(list1, list2))
implicit val order: VerifyInOrder = inOrder(ignoreStubs(list1, list2))
(there was one(list1).get(0) andThen
one(list2).get(0)).message must_== "The mock was called as expected"
}
Expand All @@ -552,7 +552,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list1.get(1)

implicit val order = inOrder(list1)
implicit val order: VerifyInOrder = inOrder(list1)

val result = there was one(list1).get(1) andThen
one(list1).get(0)
Expand All @@ -566,7 +566,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list1.get(1)

implicit val order = inOrder(list1)
implicit val order: VerifyInOrder = inOrder(list1)

var result: Result = success

Expand All @@ -584,7 +584,7 @@ The Mockito trait is reusable in other contexts
list1.get(0)
list2.get(0)

implicit val order = inOrder(list1, list2)
implicit val order: VerifyInOrder = inOrder(list1, list2)
(there was one(list2).get(0) andThen
one(list1).get(0)).message must startWith("The mock was not called as expected")
}
Expand All @@ -594,8 +594,8 @@ The Mockito trait is reusable in other contexts

list1.get(0); list1.size; list1.get(0); list1.size

implicit val order = inOrder(list1)
val result = there was one(list1).get(0) andThen
implicit val order: VerifyInOrder = inOrder(list1)
val result = there was one(list1).get(0) andThen
one(list1).size() andThen
no(list1).get(0) andThen
one(list1).size()
Expand All @@ -607,13 +607,13 @@ The Mockito trait is reusable in other contexts

def callbacks1 = {
val list = mock[java.util.List[String]]("list")
list.get(*) answers { i: Int => "The parameter is " + i.toString }
list.get(*) answers { (i: Int) => "The parameter is " + i.toString }
list.get(2) must_== "The parameter is 2"
}

def callbacks2 = {
val list = mock[java.util.List[String]]("list")
list.get(*) answers { i: Int => (i + 1).toString }
list.get(*) answers { (i: Int) => (i + 1).toString }
list.get(1) must_== "2"
list.get(5) must_== "6"
}
Expand Down
Loading