Skip to content

Instantly share code, notes, and snippets.

@noam-almog
Created January 8, 2016 14:34
Show Gist options
  • Save noam-almog/52529f272c2249816877 to your computer and use it in GitHub Desktop.
Save noam-almog/52529f272c2249816877 to your computer and use it in GitHub Desktop.
Hacking specs2 mockito matchers
import com.wixpress.commons.mail.service.HackyMockitoMatchers._
import org.specs2.execute.PendingUntilFixed._
import org.specs2.execute.{Result, Success}
import org.specs2.matcher._
import org.specs2.mock.Mockito
import org.specs2.mock.mockito.CalledMatchers
import org.specs2.mutable.SpecWithJUnit
import org.specs2.specification.Scope
class SomeTest extends SpecWithJUnit {
trait ctx extends Scope with Mockito with ThrownExpectations {
val serviceInterface = mock[ServiceInterface]
val bar = "bar"
val baz = "baz"
}
"Hacking specs2/mockito integration" should {
"old school test" in new ctx {
serviceInterface.foo( bar )
got {
one(serviceInterface).foo( bar )
no(serviceInterface).foo( baz )
}
}
"testing with matchers" in new ctx {
serviceInterface.foo( bar )
serviceInterface must { callFooWith( bar ) and not(callFooWith( baz )) }
}
"asserting failure" in new ctx {
serviceInterface.foo( baz )
serviceInterface must { callFooWith( bar ) and not(callFooWith( baz )) }
}.pendingUntilFixed("should fail")
}
}
trait ServiceInterface {
def foo(bar: String)
}
object HackyMockitoMatchers {
import CalledMatchers._
import Matchers._
def callFooWith(value: String): Matcher[ServiceInterface] =
successful ^^ { (mock: ServiceInterface) => got { one(mock).foo( value ) }.toResult }
private def successful: Matcher[Result] = beAnInstanceOf[Success]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment