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

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme