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
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
add a comment |
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
add a comment |
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
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
scala mockito
edited Mar 9 at 10:12
Manu Chadha
asked Mar 8 at 22:13
Manu ChadhaManu Chadha
3,41921945
3,41921945
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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!
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 useorg.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
add a comment |
Try any[User]
instead of any()
similar errorError:(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"
inbuld.sbt
and doimport 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
add a comment |
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
thanks Bruno. tryingmockito-scala
would be in my mind in future. At the moment, I am very new tounit-testing
and have already spent some time getting my head aroundmockito
. I'll like to develop better knowledge ofMockito
before I move tomockito-scala
.
– Manu Chadha
Mar 9 at 18:07
would I be able to mockobjects
if I usemockito-scala
. I know that is an issue withmockito
and the solution to that seem to bepowermockito
.
– 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 mockobjects
– Bruno
Mar 10 at 14:43
Ok. As far as I know,package objects
are the way to create global methods inScala
. 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!
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 useorg.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
add a comment |
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!
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 useorg.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
add a comment |
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!
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!
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 useorg.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
add a comment |
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 useorg.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
add a comment |
Try any[User]
instead of any()
similar errorError:(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"
inbuld.sbt
and doimport 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
add a comment |
Try any[User]
instead of any()
similar errorError:(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"
inbuld.sbt
and doimport 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
add a comment |
Try any[User]
instead of any()
Try any[User]
instead of any()
answered Mar 8 at 22:37
amseageramseager
1,0751917
1,0751917
similar errorError:(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"
inbuld.sbt
and doimport 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
add a comment |
similar errorError:(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"
inbuld.sbt
and doimport 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
add a comment |
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
thanks Bruno. tryingmockito-scala
would be in my mind in future. At the moment, I am very new tounit-testing
and have already spent some time getting my head aroundmockito
. I'll like to develop better knowledge ofMockito
before I move tomockito-scala
.
– Manu Chadha
Mar 9 at 18:07
would I be able to mockobjects
if I usemockito-scala
. I know that is an issue withmockito
and the solution to that seem to bepowermockito
.
– 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 mockobjects
– Bruno
Mar 10 at 14:43
Ok. As far as I know,package objects
are the way to create global methods inScala
. 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
add a comment |
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
thanks Bruno. tryingmockito-scala
would be in my mind in future. At the moment, I am very new tounit-testing
and have already spent some time getting my head aroundmockito
. I'll like to develop better knowledge ofMockito
before I move tomockito-scala
.
– Manu Chadha
Mar 9 at 18:07
would I be able to mockobjects
if I usemockito-scala
. I know that is an issue withmockito
and the solution to that seem to bepowermockito
.
– 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 mockobjects
– Bruno
Mar 10 at 14:43
Ok. As far as I know,package objects
are the way to create global methods inScala
. 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
add a comment |
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
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
answered Mar 9 at 16:07
BrunoBruno
48139
48139
thanks Bruno. tryingmockito-scala
would be in my mind in future. At the moment, I am very new tounit-testing
and have already spent some time getting my head aroundmockito
. I'll like to develop better knowledge ofMockito
before I move tomockito-scala
.
– Manu Chadha
Mar 9 at 18:07
would I be able to mockobjects
if I usemockito-scala
. I know that is an issue withmockito
and the solution to that seem to bepowermockito
.
– 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 mockobjects
– Bruno
Mar 10 at 14:43
Ok. As far as I know,package objects
are the way to create global methods inScala
. 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
add a comment |
thanks Bruno. tryingmockito-scala
would be in my mind in future. At the moment, I am very new tounit-testing
and have already spent some time getting my head aroundmockito
. I'll like to develop better knowledge ofMockito
before I move tomockito-scala
.
– Manu Chadha
Mar 9 at 18:07
would I be able to mockobjects
if I usemockito-scala
. I know that is an issue withmockito
and the solution to that seem to bepowermockito
.
– 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 mockobjects
– Bruno
Mar 10 at 14:43
Ok. As far as I know,package objects
are the way to create global methods inScala
. 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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