Skip to content

Commit 236bdeb

Browse files
committed
Fixes Issue #256
1 parent 39bde26 commit 236bdeb

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

common/src/main/scala/org/mockito/matchers/ThatMatchers.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.mockito.matchers
22

3-
import org.mockito.{ ArgumentMatcher, ArgumentMatchers => JavaMatchers }
3+
import org.mockito.{ArgumentMatcher, ArgumentMatchers => JavaMatchers}
4+
5+
import scala.util.Try
46

57
private[mockito] trait ThatMatchers {
68

@@ -16,7 +18,7 @@ private[mockito] trait ThatMatchers {
1618
*/
1719
def argThat[T](f: T => Boolean, desc: => String = "argThat(<condition>)"): T =
1820
JavaMatchers.argThat(new ArgumentMatcher[T] with Serializable {
19-
override def matches(argument: T): Boolean = f(argument)
21+
override def matches(argument: T): Boolean = Try(f(argument)).fold(_ => false, identity)
2022
override def toString: String = desc
2123
})
2224

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package user.org.mockito
2+
3+
import org.scalatest.matchers.should.Matchers
4+
import org.scalatest.wordspec.AnyWordSpec
5+
import org.mockito.{ ArgumentMatchersSugar, IdiomaticMockito, MockitoScalaSession }
6+
import IdiomaticMockito._
7+
import ArgumentMatchersSugar._
8+
9+
10+
11+
class Issue256 extends AnyWordSpec with Matchers{
12+
13+
trait Foo {
14+
def test[A](a: A): A
15+
}
16+
17+
"mockito" should {
18+
"allow stubbing the same method multiple times" in {
19+
MockitoScalaSession().run {
20+
val foo = mock[Foo]
21+
foo.test[String](argThat((s: String) => s.startsWith("foo"))) returns "foo"
22+
foo.test[Int](argThat((n: Int) => n > 10)) returns 42
23+
24+
foo.test("fooSSS") shouldBe "foo"
25+
foo.test(11) shouldBe 42
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)