scalameta / munit

Scala testing library with actionable errors and extensible APIs
https://scalameta.org/munit
Apache License 2.0
423 stars 85 forks source link

Possibly add native Scala mocking #75

Closed MrPowers closed 4 years ago

MrPowers commented 4 years ago

It might be nice to add a native Scala mocking solution to MUnit. Your scalameta expertise might help ;)

I asked about the future of mocking in Scala and it seems like most folks use mockito-scala.

Looks like mockito-scala might only work with scalatest and specs2 - not sure.

ScalaMock can only mock traits and no-args classes . It looks like they were waiting for scala.meta to be released and then the project lost steam.

I found it easier to write mocks in other languages:

Let me know what you think!

olafurpg commented 4 years ago

Thank you for reporting! I don’t have any experience with mocking so I’m not familiar with what a library like mockito provides. My understanding is that mocking is helpful in certain kinds of codebases but mocking can often be avoided by using a different design (which might be more idiomatic Scala)

MrPowers commented 4 years ago

@olafurpg - Here's a code snippet where mocking would be useful:

def sendMessageToSlack(text: String): Unit = {
  val slackApi = new SlackApi("someString")
  val slackMessage = new SlackMessage()
  slackMessage.setText(text)
  slackApi.call(slackMessage)
}

def mySillyFunction(): Int = {
  val res = 2 + 2
  sendMessageToSlack("We just computed 2 + 2!!!")
  res
}

Think you need mocking to be able to test sendMessageToSlack and mySillyFunction. In mySillyFunction, we don't actually want to run the sendMessageToSlack method. We just want to make sure that the method would be invoked with the argument "We just computed 2 + 2!!!".

I haven't used mockito-scala yet either. I will create a sample project and a blog post to discuss the key features. Will report back on this issue with my findings ;)

olafurpg commented 4 years ago

After more consideration I believe mocking is outside the scope of this library. I'm open to add integration modules with existing mocking libraries if there's demonstrable value (like with munit-scalacheck). I think any remotely interesting mocking library will quickly outgrow MUnit in complexity to merit a separate repo.

MrPowers commented 4 years ago

Sounds good, thanks for considering this.

FloWi commented 1 year ago

My understanding is that mocking is helpful in certain kinds of codebases but mocking can often be avoided by using a different design (which might be more idiomatic Scala)

Hi @olafurpg! Would you mind elaborating on that? In my scenario I have a complex trait generated by smithy4s and I would like to mock only a handful of functions for a certain test. I'd be interested in knowing how to do that the idiomatic scala way.

Edit: In case anyone else reads this who is using smithy4s - they thought about that already and you can use this feature. So my problem is solved already, but I'm still curious on how to tackle this the scala-way.