Mocking out all overlaoded versions of a method in scala mockito

Jethro

In Mockito-Scala you can stub out methods like so:

myMock.doIt(*) returns 1
myMock.doIt(*,*) returns 1
myMock.doIt(*,*)(*) returns 1

Is there a way to mock all overloaded methods at once?

Mario Galic

ScalaAnswer[T] can be used to configure mock's answers like so

object AnswerAllFoo extends ScalaAnswer[Any] {
  def answer(invocation: InvocationOnMock): Any = {
    if (invocation.getMethod.getName == "foo") 42 else ReturnsDefaults.answer(invocation)
  }
}

and then pass it in on mock creation like so

mock[Qux](AnswerAllFoo)

Here is a complete working example

import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.{ReturnsDefaults, ScalaAnswer}
import org.scalatest.{FlatSpec, Matchers}
import org.mockito.{ArgumentMatchersSugar, IdiomaticMockito}

trait Qux {
  def foo(s: Seq[Int]): Int
  def foo(i: Int, j: String): Int
  def bar(s: String): String
}

object AnswerAllFoo extends ScalaAnswer[Any] {
  def answer(invocation: InvocationOnMock): Any = {
    if (invocation.getMethod.getName == "foo") 42 else ReturnsDefaults.answer(invocation)
  }
}

class MockAnyOverload extends FlatSpec with Matchers with IdiomaticMockito with ArgumentMatchersSugar {
  "Answer[T]" should "should answer all overloaded methods foo" in {
    val qux = mock[Qux](AnswerAllFoo)

    qux.foo((Seq(1,0))) shouldBe (42)
    qux.foo(1, "zar") shouldBe (42)

    qux.bar(*) returns "corge"
    qux.bar("") shouldBe ("corge")
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mocking generic scala method in mockito

From Dev

Mockito mocking method with this parameter

From Dev

Mockito - mocking only a method

From Dev

Mockito and Guice : mocking a @Provides method

From Dev

Mocking Scala void function using Mockito

From Dev

Mocking getter / setter method of Interface with Mockito

From Dev

Mocking Joda DateTime method using Mockito

From Dev

NullPointerException in Mockito when mocking method with primitive argument

From Dev

Mocking a static method in a final class in Java/Mockito

From Dev

Mocking method with generic functional interface as an argument - Mockito

From Dev

Mockito: mocking a method call that contains a lambda expression

From Dev

Mocking method chaining with Mockito (especially a JPA TypedQuery)

From Dev

Mockito: mocking class always calls real method

From Dev

Mocking out a specific method during a unit test

From Dev

Static method and constructor interception mocking in Scala/Spark

From Dev

Static method and constructor interception mocking in Scala/Spark

From Dev

Object method mocking or spying for unit testing in Scala

From Dev

How to test async method from not mocking object with Mockito?

From Dev

Mocking a local object inside a method of SUT using Mockito or PowerMocktio

From Dev

Can I ignore aspect of a method while mocking it using Mockito?

From Dev

Mocking one method in my class during test using Mockito

From Dev

matching List in any order when mocking method behavior with Mockito

From Dev

Mocking method for all instances of class with PowerMockito

From Dev

Mocking method for all instances of class with PowerMockito

From Dev

Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

From Dev

Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

From Java

Mocking static methods with Mockito

From Dev

Mocking an enum using Mockito?

From Dev

Mocking static fields with Mockito

Related Related

  1. 1

    Mocking generic scala method in mockito

  2. 2

    Mockito mocking method with this parameter

  3. 3

    Mockito - mocking only a method

  4. 4

    Mockito and Guice : mocking a @Provides method

  5. 5

    Mocking Scala void function using Mockito

  6. 6

    Mocking getter / setter method of Interface with Mockito

  7. 7

    Mocking Joda DateTime method using Mockito

  8. 8

    NullPointerException in Mockito when mocking method with primitive argument

  9. 9

    Mocking a static method in a final class in Java/Mockito

  10. 10

    Mocking method with generic functional interface as an argument - Mockito

  11. 11

    Mockito: mocking a method call that contains a lambda expression

  12. 12

    Mocking method chaining with Mockito (especially a JPA TypedQuery)

  13. 13

    Mockito: mocking class always calls real method

  14. 14

    Mocking out a specific method during a unit test

  15. 15

    Static method and constructor interception mocking in Scala/Spark

  16. 16

    Static method and constructor interception mocking in Scala/Spark

  17. 17

    Object method mocking or spying for unit testing in Scala

  18. 18

    How to test async method from not mocking object with Mockito?

  19. 19

    Mocking a local object inside a method of SUT using Mockito or PowerMocktio

  20. 20

    Can I ignore aspect of a method while mocking it using Mockito?

  21. 21

    Mocking one method in my class during test using Mockito

  22. 22

    matching List in any order when mocking method behavior with Mockito

  23. 23

    Mocking method for all instances of class with PowerMockito

  24. 24

    Mocking method for all instances of class with PowerMockito

  25. 25

    Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

  26. 26

    Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

  27. 27

    Mocking static methods with Mockito

  28. 28

    Mocking an enum using Mockito?

  29. 29

    Mocking static fields with Mockito

HotTag

Archive