how to ignore the argument passed to a method being mockedThe mock of my class isn't getting calledWhat is the difference between mockito-core and mockito-allHow to make mock to void methods with mockitoHow can I make a method return an argument that was passed to it?Mockito mocks with Spring: “Argument passed to verify() is not a mock!”mockito callbacks and getting argument valuesMock a JPA CriteriaBuilder with MockitoMockito mock method based on another mock methodMocking method taking a Supplier<String> method not workingMocking case classes with primitive typesMockito argument matchers with higher order functionVerification on mock object not respect to test case

Mathematica command that allows it to read my intentions

How does a predictive coding aid in lossless compression?

Why do bosons tend to occupy the same state?

Im going to France and my passport expires June 19th

How writing a dominant 7 sus4 chord in RNA ( Vsus7 chord in the 1st inversion)

Should I cover my bicycle overnight while bikepacking?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

How to tell a function to use the default argument values?

Examples of smooth manifolds admitting inbetween one and a continuum of complex structures

What exploit Are these user agents trying to use?

How seriously should I take size and weight limits of hand luggage?

Which is the best way to check return result?

What about the virus in 12 Monkeys?

Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?

ssTTsSTtRrriinInnnnNNNIiinngg

Detention in 1997

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

Extract rows of a table, that include less than x NULLs

Can compressed videos be decoded back to their uncompresed original format?

How do I gain back my faith in my PhD degree?

What's the in-universe reasoning behind sorcerers needing material components?

Is there a hemisphere-neutral way of specifying a season?

Forgetting the musical notes while performing in concert

Do scales need to be in alphabetical order?



how to ignore the argument passed to a method being mocked


The mock of my class isn't getting calledWhat is the difference between mockito-core and mockito-allHow to make mock to void methods with mockitoHow can I make a method return an argument that was passed to it?Mockito mocks with Spring: “Argument passed to verify() is not a mock!”mockito callbacks and getting argument valuesMock a JPA CriteriaBuilder with MockitoMockito mock method based on another mock methodMocking method taking a Supplier<String> method not workingMocking case classes with primitive typesMockito argument matchers with higher order functionVerification on mock object not respect to test case













0















I am testing my scala and play code using Mockito. My code uses a save method which takes a User argument. I don't care about the value passed to save. I tried to code this behaviour as follows



when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



but I get error



Error:(219, 36) not found: value any
when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



What is the way to specify any for scala code in mockito?



In my build.sbt. I have downloaded only mockito-core. Do I need something else as well?



"org.mockito" % "mockito-core" % "2.24.5" % "test"










share|improve this question




























    0















    I am testing my scala and play code using Mockito. My code uses a save method which takes a User argument. I don't care about the value passed to save. I tried to code this behaviour as follows



    when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



    but I get error



    Error:(219, 36) not found: value any
    when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



    What is the way to specify any for scala code in mockito?



    In my build.sbt. I have downloaded only mockito-core. Do I need something else as well?



    "org.mockito" % "mockito-core" % "2.24.5" % "test"










    share|improve this question


























      0












      0








      0








      I am testing my scala and play code using Mockito. My code uses a save method which takes a User argument. I don't care about the value passed to save. I tried to code this behaviour as follows



      when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



      but I get error



      Error:(219, 36) not found: value any
      when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



      What is the way to specify any for scala code in mockito?



      In my build.sbt. I have downloaded only mockito-core. Do I need something else as well?



      "org.mockito" % "mockito-core" % "2.24.5" % "test"










      share|improve this question
















      I am testing my scala and play code using Mockito. My code uses a save method which takes a User argument. I don't care about the value passed to save. I tried to code this behaviour as follows



      when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



      but I get error



      Error:(219, 36) not found: value any
      when(mockUserRepository.save(any())).thenReturn(Future(Some(user)))



      What is the way to specify any for scala code in mockito?



      In my build.sbt. I have downloaded only mockito-core. Do I need something else as well?



      "org.mockito" % "mockito-core" % "2.24.5" % "test"







      scala mockito






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 10:12







      Manu Chadha

















      asked Mar 8 at 22:13









      Manu ChadhaManu Chadha

      3,41921945




      3,41921945






















          3 Answers
          3






          active

          oldest

          votes


















          0














          You can use org.mockito.Matchers



          import org.mockito.Mockito._
          import org.mockito.Matchers._

          val mockUserRepository = mock[call_your_MockUserRepositiry_service]
          // something like below
          // val service = mock[Service[Any, Any]] OR
          // val mockService = mock[MyService]

          when(mockUserRepository.save(any)) thenReturn(Future(Some(user)))


          Please refer https://www.programcreek.com/scala/org.mockito.Matchers



          Update:



          If Matchers are deprecated in Mockito 2.0 then you can use org.mockito.ArgumentMatchers



          In Java Something like below



          class Foo
          boolean bool(String str, int i, Object obj)
          return false;



          Foo mockFoo = mock(Foo.class);
          when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);


          In Scala something like below



          def setupService(inputResponse: Future[Unit]): AdminService = 
          val mockConnector = mock[MongoConnector]

          when(mockConnector.putEntry(ArgumentMatchers.any(), ArgumentMatchers.any())(ArgumentMatchers.any()))
          .thenReturn(inputResponse)

          new AdminService(mockConnector)



          Hope it helps!






          share|improve this answer

























          • Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

            – Manu Chadha
            Mar 9 at 14:10











          • Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

            – KZapagol
            Mar 9 at 14:38











          • Please refer my updated comment above.

            – KZapagol
            Mar 9 at 14:45



















          0














          Try any[User] instead of any()






          share|improve this answer























          • similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

            – Manu Chadha
            Mar 9 at 9:18











          • to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

            – Manu Chadha
            Mar 9 at 10:36


















          0














          I'd say that to avoid this and many other issues related to Scala-Java interoperability, you should use the Scala version of Mockito (mockito-scala) with it, after mix-in the trait org.mockito.ArgumentMatchersSugar you can write



          when(mockUserRepository.save(*)).thenReturn(Future(Some(user)))


          Or if you fancy a more scala-like syntax



          mockUserRepository.save(*) shouldReturn Future(Some(user))


          Check the readme to see more examples and scala specific features






          share|improve this answer























          • thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

            – Manu Chadha
            Mar 9 at 18:07











          • would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

            – Manu Chadha
            Mar 9 at 18:47











          • You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

            – Bruno
            Mar 10 at 14:43












          • Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

            – Manu Chadha
            Mar 10 at 15:17











          • Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

            – Bruno
            Mar 10 at 18:34











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071728%2fhow-to-ignore-the-argument-passed-to-a-method-being-mocked%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use org.mockito.Matchers



          import org.mockito.Mockito._
          import org.mockito.Matchers._

          val mockUserRepository = mock[call_your_MockUserRepositiry_service]
          // something like below
          // val service = mock[Service[Any, Any]] OR
          // val mockService = mock[MyService]

          when(mockUserRepository.save(any)) thenReturn(Future(Some(user)))


          Please refer https://www.programcreek.com/scala/org.mockito.Matchers



          Update:



          If Matchers are deprecated in Mockito 2.0 then you can use org.mockito.ArgumentMatchers



          In Java Something like below



          class Foo
          boolean bool(String str, int i, Object obj)
          return false;



          Foo mockFoo = mock(Foo.class);
          when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);


          In Scala something like below



          def setupService(inputResponse: Future[Unit]): AdminService = 
          val mockConnector = mock[MongoConnector]

          when(mockConnector.putEntry(ArgumentMatchers.any(), ArgumentMatchers.any())(ArgumentMatchers.any()))
          .thenReturn(inputResponse)

          new AdminService(mockConnector)



          Hope it helps!






          share|improve this answer

























          • Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

            – Manu Chadha
            Mar 9 at 14:10











          • Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

            – KZapagol
            Mar 9 at 14:38











          • Please refer my updated comment above.

            – KZapagol
            Mar 9 at 14:45
















          0














          You can use org.mockito.Matchers



          import org.mockito.Mockito._
          import org.mockito.Matchers._

          val mockUserRepository = mock[call_your_MockUserRepositiry_service]
          // something like below
          // val service = mock[Service[Any, Any]] OR
          // val mockService = mock[MyService]

          when(mockUserRepository.save(any)) thenReturn(Future(Some(user)))


          Please refer https://www.programcreek.com/scala/org.mockito.Matchers



          Update:



          If Matchers are deprecated in Mockito 2.0 then you can use org.mockito.ArgumentMatchers



          In Java Something like below



          class Foo
          boolean bool(String str, int i, Object obj)
          return false;



          Foo mockFoo = mock(Foo.class);
          when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);


          In Scala something like below



          def setupService(inputResponse: Future[Unit]): AdminService = 
          val mockConnector = mock[MongoConnector]

          when(mockConnector.putEntry(ArgumentMatchers.any(), ArgumentMatchers.any())(ArgumentMatchers.any()))
          .thenReturn(inputResponse)

          new AdminService(mockConnector)



          Hope it helps!






          share|improve this answer

























          • Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

            – Manu Chadha
            Mar 9 at 14:10











          • Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

            – KZapagol
            Mar 9 at 14:38











          • Please refer my updated comment above.

            – KZapagol
            Mar 9 at 14:45














          0












          0








          0







          You can use org.mockito.Matchers



          import org.mockito.Mockito._
          import org.mockito.Matchers._

          val mockUserRepository = mock[call_your_MockUserRepositiry_service]
          // something like below
          // val service = mock[Service[Any, Any]] OR
          // val mockService = mock[MyService]

          when(mockUserRepository.save(any)) thenReturn(Future(Some(user)))


          Please refer https://www.programcreek.com/scala/org.mockito.Matchers



          Update:



          If Matchers are deprecated in Mockito 2.0 then you can use org.mockito.ArgumentMatchers



          In Java Something like below



          class Foo
          boolean bool(String str, int i, Object obj)
          return false;



          Foo mockFoo = mock(Foo.class);
          when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);


          In Scala something like below



          def setupService(inputResponse: Future[Unit]): AdminService = 
          val mockConnector = mock[MongoConnector]

          when(mockConnector.putEntry(ArgumentMatchers.any(), ArgumentMatchers.any())(ArgumentMatchers.any()))
          .thenReturn(inputResponse)

          new AdminService(mockConnector)



          Hope it helps!






          share|improve this answer















          You can use org.mockito.Matchers



          import org.mockito.Mockito._
          import org.mockito.Matchers._

          val mockUserRepository = mock[call_your_MockUserRepositiry_service]
          // something like below
          // val service = mock[Service[Any, Any]] OR
          // val mockService = mock[MyService]

          when(mockUserRepository.save(any)) thenReturn(Future(Some(user)))


          Please refer https://www.programcreek.com/scala/org.mockito.Matchers



          Update:



          If Matchers are deprecated in Mockito 2.0 then you can use org.mockito.ArgumentMatchers



          In Java Something like below



          class Foo
          boolean bool(String str, int i, Object obj)
          return false;



          Foo mockFoo = mock(Foo.class);
          when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);


          In Scala something like below



          def setupService(inputResponse: Future[Unit]): AdminService = 
          val mockConnector = mock[MongoConnector]

          when(mockConnector.putEntry(ArgumentMatchers.any(), ArgumentMatchers.any())(ArgumentMatchers.any()))
          .thenReturn(inputResponse)

          new AdminService(mockConnector)



          Hope it helps!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 9 at 15:47

























          answered Mar 9 at 13:24









          KZapagolKZapagol

          54618




          54618












          • Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

            – Manu Chadha
            Mar 9 at 14:10











          • Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

            – KZapagol
            Mar 9 at 14:38











          • Please refer my updated comment above.

            – KZapagol
            Mar 9 at 14:45


















          • Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

            – Manu Chadha
            Mar 9 at 14:10











          • Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

            – KZapagol
            Mar 9 at 14:38











          • Please refer my updated comment above.

            – KZapagol
            Mar 9 at 14:45

















          Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

          – Manu Chadha
          Mar 9 at 14:10





          Thanks. I have read that ‘Matchers’ are deprecated from ‘Mockito 2.0’. Is there an alternate approach?

          – Manu Chadha
          Mar 9 at 14:10













          Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

          – KZapagol
          Mar 9 at 14:38





          Then you can use org.mockito.ArgumentMatcher to mock the behavior for any argument of the given type.

          – KZapagol
          Mar 9 at 14:38













          Please refer my updated comment above.

          – KZapagol
          Mar 9 at 14:45






          Please refer my updated comment above.

          – KZapagol
          Mar 9 at 14:45














          0














          Try any[User] instead of any()






          share|improve this answer























          • similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

            – Manu Chadha
            Mar 9 at 9:18











          • to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

            – Manu Chadha
            Mar 9 at 10:36















          0














          Try any[User] instead of any()






          share|improve this answer























          • similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

            – Manu Chadha
            Mar 9 at 9:18











          • to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

            – Manu Chadha
            Mar 9 at 10:36













          0












          0








          0







          Try any[User] instead of any()






          share|improve this answer













          Try any[User] instead of any()







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 22:37









          amseageramseager

          1,0751917




          1,0751917












          • similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

            – Manu Chadha
            Mar 9 at 9:18











          • to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

            – Manu Chadha
            Mar 9 at 10:36

















          • similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

            – Manu Chadha
            Mar 9 at 9:18











          • to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

            – Manu Chadha
            Mar 9 at 10:36
















          similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

          – Manu Chadha
          Mar 9 at 9:18





          similar error Error:(221, 36) not found: value any when(mockUserRepository.save(any[User])).thenReturn(Future(Some(user)))

          – Manu Chadha
          Mar 9 at 9:18













          to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

          – Manu Chadha
          Mar 9 at 10:36





          to make any[User] work, I had to include "org.mockito" %"mockito-all"%"1.10.19"%"test" instead of "org.mockito" % "mockito-core" % "2.24.5" % "test" in buld.sbt and do import org.mockito.Matchers.any in the spec file. I am not sure if this is the right way though

          – Manu Chadha
          Mar 9 at 10:36











          0














          I'd say that to avoid this and many other issues related to Scala-Java interoperability, you should use the Scala version of Mockito (mockito-scala) with it, after mix-in the trait org.mockito.ArgumentMatchersSugar you can write



          when(mockUserRepository.save(*)).thenReturn(Future(Some(user)))


          Or if you fancy a more scala-like syntax



          mockUserRepository.save(*) shouldReturn Future(Some(user))


          Check the readme to see more examples and scala specific features






          share|improve this answer























          • thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

            – Manu Chadha
            Mar 9 at 18:07











          • would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

            – Manu Chadha
            Mar 9 at 18:47











          • You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

            – Bruno
            Mar 10 at 14:43












          • Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

            – Manu Chadha
            Mar 10 at 15:17











          • Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

            – Bruno
            Mar 10 at 18:34















          0














          I'd say that to avoid this and many other issues related to Scala-Java interoperability, you should use the Scala version of Mockito (mockito-scala) with it, after mix-in the trait org.mockito.ArgumentMatchersSugar you can write



          when(mockUserRepository.save(*)).thenReturn(Future(Some(user)))


          Or if you fancy a more scala-like syntax



          mockUserRepository.save(*) shouldReturn Future(Some(user))


          Check the readme to see more examples and scala specific features






          share|improve this answer























          • thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

            – Manu Chadha
            Mar 9 at 18:07











          • would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

            – Manu Chadha
            Mar 9 at 18:47











          • You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

            – Bruno
            Mar 10 at 14:43












          • Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

            – Manu Chadha
            Mar 10 at 15:17











          • Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

            – Bruno
            Mar 10 at 18:34













          0












          0








          0







          I'd say that to avoid this and many other issues related to Scala-Java interoperability, you should use the Scala version of Mockito (mockito-scala) with it, after mix-in the trait org.mockito.ArgumentMatchersSugar you can write



          when(mockUserRepository.save(*)).thenReturn(Future(Some(user)))


          Or if you fancy a more scala-like syntax



          mockUserRepository.save(*) shouldReturn Future(Some(user))


          Check the readme to see more examples and scala specific features






          share|improve this answer













          I'd say that to avoid this and many other issues related to Scala-Java interoperability, you should use the Scala version of Mockito (mockito-scala) with it, after mix-in the trait org.mockito.ArgumentMatchersSugar you can write



          when(mockUserRepository.save(*)).thenReturn(Future(Some(user)))


          Or if you fancy a more scala-like syntax



          mockUserRepository.save(*) shouldReturn Future(Some(user))


          Check the readme to see more examples and scala specific features







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 at 16:07









          BrunoBruno

          48139




          48139












          • thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

            – Manu Chadha
            Mar 9 at 18:07











          • would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

            – Manu Chadha
            Mar 9 at 18:47











          • You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

            – Bruno
            Mar 10 at 14:43












          • Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

            – Manu Chadha
            Mar 10 at 15:17











          • Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

            – Bruno
            Mar 10 at 18:34

















          • thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

            – Manu Chadha
            Mar 9 at 18:07











          • would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

            – Manu Chadha
            Mar 9 at 18:47











          • You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

            – Bruno
            Mar 10 at 14:43












          • Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

            – Manu Chadha
            Mar 10 at 15:17











          • Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

            – Bruno
            Mar 10 at 18:34
















          thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

          – Manu Chadha
          Mar 9 at 18:07





          thanks Bruno. trying mockito-scala would be in my mind in future. At the moment, I am very new to unit-testing and have already spent some time getting my head around mockito. I'll like to develop better knowledge of Mockito before I move to mockito-scala.

          – Manu Chadha
          Mar 9 at 18:07













          would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

          – Manu Chadha
          Mar 9 at 18:47





          would I be able to mock objects if I use mockito-scala. I know that is an issue with mockito and the solution to that seem to be powermockito.

          – Manu Chadha
          Mar 9 at 18:47













          You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

          – Bruno
          Mar 10 at 14:43






          You wouldn't, it's usually a hint that something's wrong with your design if you need to mock objects

          – Bruno
          Mar 10 at 14:43














          Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

          – Manu Chadha
          Mar 10 at 15:17





          Ok. As far as I know, package objects are the way to create global methods in Scala. I am wondering how I can mock such methods then. If you don't mind, could you please take a look at this stackoverflow.com/questions/55089128/…

          – Manu Chadha
          Mar 10 at 15:17













          Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

          – Bruno
          Mar 10 at 18:34





          Yes, but those global functions are usually pure functions for small utilities, if you have something complex enough that need to be mocked for the sake of testing, you should model it as a trait/class and then inject the dependency (either as a constructor param or as an extra param to the function that uses it) so then in test code you can replace it by a mock

          – Bruno
          Mar 10 at 18:34

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid …


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071728%2fhow-to-ignore-the-argument-passed-to-a-method-being-mocked%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page