Scala Mockito: allow only certain (i.e. whitelist) methods to be called

Related searches

How can I create a mock and "white list" only certain methods?

For example, if I have a trait Foo, I want to allow only calls to a() and b(), and disallow any other methods -- without explicitly blacklisting them.

trait Foo {
    def a()
    def b()
    def c()
    def d()
}

val mfoo = mock[Foo]
whitelist(mfoo.a, mfoo.b)
mfoo.c() // should fail since it's not whitelisted

Use strict mode with mockito-scala-scalatest, when you work in strict mode any method you don't stub/verify and it's called by your code will make the test fail with an exception stating you shouldn't be calling said method

mockito-scala, to should only be used on stubbed methods, and can't express expectations about mockito-scala being the core; mockito-scala-scalatest having specific classes that no no special syntax is needed, so if you were using eqTo with varargs, i.e. The companion object also extends the trait to allow the usage of the API� aMock.method(5) aMock.method(n > 4.99) was called aMock.method(n >= 5) was called aMock.method(n < 5.1) was called aMock.method(n <= 5) was called Varargs Most matchers that makes sense to, work with varargs out of the box, the only thing to notice is that if you are passing more than one value and want to use eqTo then you should pass all of

When I used Mockito I used the verifyNoMoreInteractions().

You can stub your mock and then you can verify that no other invokation has happened.

In the current version there are also other verifications methods. Have a look here.

Testing with mock objects, For example, here's how you'd state that you expect your mock to be called once Proxy mocks can only be used to mock Scala traits and Java interfaces. If no expected arguments are given, mocks accept any arguments. By default, mocks expect one or more calls (i.e., only fail if the function or method is never called). Mockito for Scala language. Identify your strengths with a free online coding quiz, and skip resume and recruiter screens at multiple companies at once.

For clarity's sake:

You can use verify(mfoo, atLeast(0)).a() to allow zero or more invocations of method a(), thus whitelisting it.

trait Foo {
    def a()
    def b()
    def c()
    def d()
}

val mfoo = mock[Foo]

// ... test code ...

// whitelisting a() and b() is equivalent to:
verify(mfoo, atLeast(0)).a()
verify(mfoo, atLeast(0)).b()

// everything else is blacklisted by default:
verifyNoMoreInteractions(mfoo)

p.s. You can also hook into any invocation on the mock object by using the Answer construct (Mockito's Answer in ScalaTest), but I was not successful in doing so in my code.

Mocking a dynamic class method argument using scalatest/mockiton , so, getSomeValue is a method i can call only in production, it comes with a 3rd party we rename this because Scala defines `eq` on `AnyRef` import org. mockito. If you find yourself having to mock out some functionality of your class while Turn to PowerMockito or JMockit; frameworks that allow to mock calls to new. how to mock internal method call using mockito/powermockito. java,junit,mockito,powermock. Null is returned because mocked method is never called: In your example when mocking you are passing a mockObject as a matcher, as a result mocked response is only returned when said method is invoked using the same mockObject as a parameter.

Cannot use futures in scala.js, When I am using futures in Scala.js, at least in tests, they are never executed and I testing frameworks support Futures in a special way, by allowing a test itself to due to a mistake in the Actor code I call an unexpected method in the mock. foo only when it receives a message") { val mockAPI = mock[SomeAPI] val ref� how to mock internal method call using mockito/powermockito. java,junit,mockito,powermock. Null is returned because mocked method is never called: In your example when mocking you are passing a mockObject as a matcher, as a result mocked response is only returned when said method is invoked using the same mockObject as a parameter.

Web Site Whitelisting in Microsoft Internet Explorer, By using this technique, IE will be restricted from accessing web sites that are not explicitly on the whitelist. Whitelisting in IE using a proxy auto-configuration (PAC ) file clicking the "cog" icon in the upper right hand corner of some versions of IE. of IE into a hidden frame before checking access to the web site is allowed. To run only one test class, run test-only followed by the name of the class i.e. test-only my.namespace.MySpec. To run only the tests that have failed, run test-quick. To run tests continually, run a command with a tilde in front, i.e. ~test-quick.

Mockito keeps track of all the method calls and their parameters to the mock object. You can use the verify() method on the mock object to verify that the specified conditions are met. For example, you can verify that a method has been called with certain parameters. This kind of testing is sometimes called behavior testing. Behavior testing

Comments
  • Does this answer your question? Is it possible to do strict mocks with Mockito?
  • @EyalRoth doesn't seem to stackoverflow.com/questions/15024352/…
  • @solyd I am not sure what you meant by referencing that link, but if you think it says you can't use Answer, it is certainly not so: you very much can use it.
  • @Dima I would love an example for my simple use case because I can't get it to work.
  • @solyd what exactly do you mean by "can't get it to work"? Did you try just doing what that SO answer suggests?
  • This doesn't not solve my problem as verify(x).foo() is equivalent to verify(x, times(1)).foo(). I want to merely allow invocation of foo, not ensure it was called at least once.
  • I don't see the problem: val m= mock[Foo]; m.a(); m.b(); m.c(); verify(m, atLeastOnce(),).a(); verify(m, atMostOnce()).b();verifyNoMoreInteractions(m) // <- this should fail as c() is called
  • @thopaw the problem is this will fail if a() is not called
  • @thopaw Use atLeast(0).