I'd like to reuse mock declarations accross tests (if possible).
Here is a minimal non-working example using ScalaTest and Mockito. I'm expecting the yes value in the first test but I get the other value.
It seems that the latest Mockito.when is the one applied for all test clauses.
Is there a way to avoid declaring mocks in each in clause?
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
class ReuseMocksSpec extends WordSpec with Matchers with MockitoSugar {
"A test" when {
val service = mock[Service]
"sharing mocks among tests" should {
when(service.getVal).thenReturn("yes")
"get yes value" in {
service.getVal should be("yes")
}
}
"sharing mocks among other tests" should {
when(service.getVal).thenReturn("other")
"get other value" in {
service.getVal should be("other")
}
}
}
trait Service {
def getVal: String
}
}
I reviewed the way I designed it and am now using a function to build my mocks:
def withValue(value: String)(body: (Service => String)) = {
val service = mock[Service]
when(service.getVal).thenReturn(value)
body(service)
}
The test class would become:
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{Matchers, WordSpec}
class ReuseMocksSpec extends WordSpec with Matchers with MockitoSugar {
"A test" when {
"sharing mocks among tests" should {
"get yes value" in {
val value = withValue("yes") { service =>
service.getVal
}
value should be("yes")
}
}
"sharing mocks among other tests" should {
"get other value" in {
val value = withValue("other") { service =>
service.getVal
}
value should be("other")
}
}
}
def withValue(value: String)(body: (Service => String)) = {
val service = mock[Service]
when(service.getVal).thenReturn(value)
body(service)
}
trait Service {
def getVal: String
}
}
I don't know if it's the cleanest and easiest way to do it but it works...
Related
I'm writing Specs2 tests that use a temporary file and properties from ScalaCheck. Without properties it works fine :
import better.files.File
import org.specs2.execute.{AsResult, Result}
import org.specs2.mutable.Specification
import org.specs2.specification.ForEach
trait TmpDirContext extends ForEach[File] {
def foreach[R: AsResult](testWithFile: File => R): Result = {
val tmpDirCtx = File.temporaryDirectory()
AsResult(tmpDirCtx.apply(testWithFile))
}
}
class OkTest extends Specification with TmpDirContext {
import better.files._
"Example" should {
"work" in { tmpDir: File =>
tmpDir.exists must beTrue
}
}
}
val test = new OkTest
specs2.run(test)
If I add properties, it doesn't compile :
import org.scalacheck.Prop
import org.specs2.ScalaCheck
class KoTest extends Specification with ScalaCheck with TmpDirContext {
"KoTest" should {
"work" in { tmpDir: File =>
"for" ! Prop.forAll { value: Int =>
tmpDir.exists must beTrue
}
}
}
Error:(26, 16) could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[better.files.File => org.specs2.specification.core.Fragment]
"work" in { tmpDir: File =>
I've managed to make it compile, but then the test fails seemingly because the ForEach from TmpDirContext has already disposed of a temporary folder:
class KoTest2 extends Specification with ScalaCheck with TmpDirContext {
"KoTest2" should {
"work" >> { tmpDir: File =>
Prop.forAll { value: Int =>
tmpDir.exists must beTrue
}
}
}
}
I guess I'm missing something... How to make it work and have the tmpDir available in the property testing?
You can try the following approach
import org.specs2.mutable.Specification
import java.io.File
import org.specs2.ScalaCheck
import org.scalacheck._
class KoTest extends Specification with ScalaCheck with TempDir { sequential
"KoTest" should {
"work" >> {
"for" >> prop { (tmpDir: File, value: Int) =>
tmpDir.exists must beTrue
}.after(deleteTmpDir)
}
}
}
trait TempDir {
implicit def arbitraryTempDir: Arbitrary[File] =
Arbitrary(tmpDir)
val tmpDir = new File("temp")
def deleteTmpDir = tmpDir.delete
}
It is presented here
I have a custom directive with a function like the following that returns a Directive1[ValidatedParameters], where ValidatedParameters is just a simple case class:
class MyCustomDirective {
def validateParameters(...): Directive1[ValidatedParameters] = {
...
provide(ValidatedParameters(...))
}
}
I'm using it like this in my route:
myCustomDirective.validateParameters(top, skip, modifiedDate) {
(validatedParameters: ValidatedParameters) => {
However, I have a unit test where I'd basically like to call the above function and verify that ValidatedParameters is what I expect:
val actualResult: Directive1[ValidatedParameters] = new MyCustomDirective().validateParameters(...)
So actualResult is a Directive1[ValidatedParameters], is there a way I can get access to the ValidatedParameters case class within this directive from a unit test?
Here is a solution for writing a test case for the custom directive as mentioned here:
REQUEST ~> ROUTE ~> check {
ASSERTIONS
}
For a custom directive to get page and per_page query paramters:
case class Paginate(page: Option[String], perPage: Option[String])
trait PaginationDirective extends ParameterDirectives with RespondWithDirectives {
def withPagination: Directive1[Paginate] = {
parameters(
(
"page".as[String].?,
"per_page".as[String].?
)
).tmap { case (pageOpt, perPageOpt) => Paginate(pageOpt, perPageOpt) }
}
def logCurrentPage(currentPage: Int): Directive0 = {
mapResponse(response => {
logger.info(s"currentPage is $currentPage")
response
})
}
}
Test case for the above custom directive:
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}
class PaginationDirectiveTest extends WordSpec with Matchers with PaginationDirective with ScalatestRouteTest {
"paginate directive" should {
"return page and per page parameters" in {
Get("/?page=1&per_page=25") ~> withPagination(i => complete(i.toString)) ~> check {
responseAs[String] shouldEqual "[Paginate(Some(1), Some(25))]"
}
}
"logCurrentPage" should {
Get("/hello") ~> logCurrentPage(5)(complete("Dummy msg")) ~> check {
//validate test here.
}
}
}
I ended up with the following:
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.testkit.ScalatestRouteTest
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport._
import org.scalamock.scalatest.MockFactory
import org.scalatest.{BeforeAndAfter, Matchers}
class MyTest extends ScalatestRouteTest {
...
"validate(top=None, skip=None, modifiedDate=None)" should "pass validation" in {
myCustomDirective.validate(top, skip, modifiedDate) {
(validatedParameters: ValidatedParameters) => {
validatedParameters shouldEqual expectedResult
complete("") // Needed to pass compilation
}
}
}
}
I am writing unit test cases using specs2 and my application is started and stopped for each test instances.
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in new WithApplication {
"Hello world" must have size(11)
}
"start with 'Hello'" in new WithApplication {
"Hello world" must startWith("Hello")
}
"end with 'world'" in new WithApplication {
"Hello world" must endWith("world")
}
}
}
As mentioned in the documentation for each test case application is started and stopped.
I have found a workaround from the link. Application initializes only once (I haven' tested it yet) for each Test Class.
import org.specs2.mutable._
class HelloWorldSpec extends Specification {sequential
step(Play.start(App)) //supposedly App is iniatilized
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
step(Play.stop())
}
But what if we have multiple classes and we want a single start and stop of the app.
import org.specs2.mutable._
class HelloWorldSpec extends Specification {sequential
step(Play.start(App)) //supposedly App is iniatilized
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
step(Play.stop())
}
import org.specs2.mutable._
class HitchHikerSpec extends Specification {sequential
step(Play.start(App)) //supposedly App is iniatilized
"The 'Hitch Hiker' string" should {
"contain 11 characters" in {
"Hitch Hiker" must have size(11)
}
"start with 'Hitch'" in {
"Hitch Hiker" must startWith("Hitch")
}
"end with 'Hiker'" in {
"Hitch Hiker" must endWith("Hiker")
}
}
step(Play.stop())
}
How would I start and stop app for once?
There is a similar solution implemented in scalatest using OneAppPerSuite.
Here is the link and example.
import play.api.test._
import org.scalatest._
import org.scalatestplus.play._
import play.api.{Play, Application}
import play.api.inject.guice._
// This is the "master" suite
class NestedExampleSpec extends Suites(
new OneSpec,
new TwoSpec,
new RedSpec,
new BlueSpec
) with OneAppPerSuite {
// Override app if you need an Application with other than non-default parameters.
implicit override lazy val app: Application =
new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build()
}
// These are the nested suites
#DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp
#DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp
#DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp
#DoNotDiscover
class BlueSpec extends PlaySpec with ConfiguredApp {
"The OneAppPerSuite trait" must {
"provide an Application" in {
app.configuration.getString("ehcacheplugin") mustBe Some("disabled")
}
"make the Application available implicitly" in {
def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key)
getConfig("ehcacheplugin") mustBe Some("disabled")
}
"start the Application" in {
Play.maybeApplication mustBe Some(app)
}
}
}
Can something similar be implemented in specs2?
With specs2 you can do something similar with specification references:
class SuiteSpec extends Specification { def is = s2"""
${link(StartSpec).hide}
${ "first spec" ~ new Spec1Spec }
${ "second spec" ~ new Spec2Spec }
${link(StopSpec).hide}
"""
}
object StartSpec extends Specification { def is = s2"""
${step(println("start"))}
"""
}
class Spec1Spec extends Specification { def is = s2"""
example1 $e1
"""
def e1 = { println("example1"); ok }
}
class Spec2Spec extends Specification { def is = s2"""
example2 $e2
"""
def e2 = { println("example2"); ok }
}
object StopSpec extends Specification { def is = s2"""
${step(println("stop"))}
"""
}
Then if you run:
testOnly *Suite* -- all
You should see the following lines printed out:
start
example1
example2
stop
I've been noticing a lot of repetition to set up each test with Play/specs2. I'm aware that you can create a trait that extends mutable.Before and use that to set up tests but any values created there seem to be out of scope for my tests.
What I would like to be able to do is set val user = User.create("User Name") in a class or trait before each test runs so I can have access to user later on in the test. How can this be achieved?
// SpecificationWithFixtures.scala
package models
import org.specs2.execute.{AsResult, Result}
import org.specs2.mutable._
import play.api.test.Helpers._
import play.api.test._
abstract class SpecificationWithFixtures extends Specification {
abstract class WithFakeDB extends WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
override def around[T: AsResult](t: => T): Result = super.around {
t
}
}
}
// UserSpec.scala
package models
import org.junit.runner._
import org.specs2.runner._
#RunWith(classOf[JUnitRunner])
class UserSpec extends SpecificationWithFixtures {
"User.create" should {
"save user in the database" in new WithFakeDB {
val user = User.create("User Name")
// Some test for user
}
}
"User.findAll" should {
"return a list of all users" in new WithFakeDB {
val user = User.create("User Name")
// Another test for user
}
}
}
What you can do is something like:
def withUser[T](test: User => T): T = test(User create "Username")
// or even more configurable
def withUser[T](name: String)(test: User => T): T = test(User create name)
// Then writing expectations you can do
"User Roger" in withUser("Roger") {
roger => // trivial example
roger.name must_== "Roger"
}
// or even
"User" in withUser("John") {
_.name must_== "John"
}
This kind of loan pattern is useful writing specs2.
In previous example it's user per expectation (in), but it can be used for a group of expectations (should, >>), or for all.
"User" should withUser("xyz") {
"exp1" in { ??? }
}
Suppose I have a specs2 specification defined in the "unit" style as follows:
import org.specs2.mutable
class MyClassSpec extends mutable.Specification {
"myMethod" should {
"return positive values" in {
MyClass.myMethod must beGreaterThan(0)
}
"return values less than 100" in {
MyClass.myMethod must beLessThan(100)
}
}
}
Is there an easy way to skip/disable/mark pending all of the examples within the should block/fragment for myMethod?
Obviously I can call pendingUntilFixed or return pending from each individual example in the block, but this would be rather tedious for a block with many specifications.
It seems like this would be a common occurrence if MyClass.myMethod is difficult to implement and gets punted. Is there another way that this is commonly done in specs2?
You can mix in the Tags trait and define any section you want:
import org.specs2.mutable._
class MyClassSpec extends Specification with Tags {
section("pending")
"myMethod" should {
"return positive values" in {
MyClass.myMethod must beGreaterThan(0)
}
"return values less than 100" in {
MyClass.myMethod must beLessThan(100)
}
}
section("pending")
}
Then you run your specification with exclude pending
>test-only *MyClassSpec* -- exclude pending
This is documented here.
You can also use an implicit context to make sure that all your examples in the should block are PendingUntilFixed:
import org.specs2._
import execute._
class MyClassSpec extends mutable.Specification {
"this doesn't work for now" >> {
implicit val puf = pendingContext("FIXME")
"ex1" in ko
"ex2" in ok
}
"but this works ok" >> {
"ex3" in ko // maybe not here ;-)
"ex4" in ok
}
def pendingContext(reason: String) = new mutable.Around {
def around[T <% Result](t: =>T) =
t.pendingUntilFixed(reason)
}
}
Update for specs2 3.x
import org.specs2._
import execute._
class TestMutableSpec extends mutable.Specification {
"this doesn't work for now" >> {
implicit def context[T] = pendingContext[T]("FIXME")
"ex1" in ko
"ex2" in ok
}
"but this works ok" >> {
"ex3" in ko // maybe not here ;-)
"ex4" in ok
}
def pendingContext[T](reason: String): AsResult[MatchResult[T]] =
new AsResult[MatchResult[T]] {
def asResult(t: =>MatchResult[T]): Result =
AsResult(t).pendingUntilFixed(reason)
}
}