Invalid use of argument matchers! (Mockito) - mockito

I got an error when I was using Mockito.
I use the matchers for all parameters, but an error has occurred.

Related

ActiveStorage: TypeError Exception: no implicit conversion of String into Hash

After Rails update from 6.1 to 7.0.3, I start getting ActiveStorage errors while calling something like
object.document_attachment.url
it throws the error *** TypeError Exception: no implicit conversion of String into Hash
One quick solution is
Rails.application.routes.url_helpers.rails_blob_path(object.document_attachment.attachment, only_path: true)
but I am not in the favor of that, looking for the actual reason for the change.

PowerMockito Argument Matching failure

I am trying to use PowerMockito to mock a private class like this:
PowerMockito.when(spyClass, "privateMethod", anyString()).thenReturn(returnList);
I have these two annotations above the test method:
#Test
#PrepareForTest(CartItemRepository.class)
And these above the class:
#ExtendWith(MockitoExtension.class)
#RunWith(PowerMockRunner.class)
There are no previous method calls (stubbed or otherwise) in the test method that could be interfering.
But, I am getting an error related to incorrect use of argument matchers.
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
I am using the when method of PowerMockito (and not regular Mockito) but the 2nd error paragraph makes it seem like mocking private methods is still not possible.
I'm not sure what I'm doing wrong here.

Mockito verify method is called ignoring parameter

I have a DAO method I want to verify is called inside Service
send(User user, Properties prop)
I can verify using a protected method in service, but I think it should be private
verify(dao).send(user, service.getProp())
I tried in different ways to define accepting any properties as:
verify(dao).send(user, any(Properties.class)); // or any()
verify(dao).send(user, Matchers.isA(Properties.class)));
But all failed with invalid arguments
FAILED: testService
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.package.TestService.testService(TestService.java:330)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
As the exception details explain, you're not allowed to mix raw values (user) with matchers (any(...)).
Instead, use matchers for all arguments using the eq(...) matcher:
verify(dao).send(eq(user), any());

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito

getting the error
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
at
when(
getProgramService
.callService(any(GetProgramType.class)))
.thenReturn(jAXBresponse);
please explain the error and possible resolution
The code you posted looks fine; I don't see a problem with it. It is likely the code immediately before or above it causing an exception. The key is here:
Invalid use of argument matchers! 1 matchers expected, 2 recorded.
any, as you used it above, doesn't return a "special kind of null" or "special kind of GetProgramType"; those don't exist. Instead, it returns null and as a side effect puts a matcher on a stack. When checking for matchers, Mockito looks at the stack, and checks that it is either empty (i.e. check equality with all arguments) or exactly equal to the number of arguments in the call you're checking (i.e. there is a matcher for each argument).
What's happening here is that you're getting one more matcher than the callService method expects. I see this often enough when developers mistakenly try to save a matcher in a local variable:
String expectedString = Matchers.anyString(); // DOESN'T WORK
// Instead, this just adds a matcher to the stack, almost certainly where it
// doesn't belong.
...or mock a final method:
// Assume getProgramService.otherMethod is final.
when(getProgramService.otherMethod(anyString())).thenReturn(123L);
// This actually calls getProgramService.otherMethod(null) and leaves an
// anyString matcher on the stack without setting any expectation that
// would returns 123L.
To confirm this is a problem, temporarily add this statement immediately before the one you posted:
Mockito.validateMockitoUsage();
This artificially checks the stack for matchers, and will likely throw an exception on that line. If it does, check the code above your post. Either way, adding some surrounding code to your question will likely help us debug it.

com.facebook.presto.server.PrestoServer Guice creation errors

I got the following error when trying to start presto server, how can I diagnose what property is malformed or something else? Thanks.
config file:
coordinator=true
datasources=jmx
http-server.http.port=8080
presto-metastore.db.type=h2
presto-metastore.db.filename=var/db/MetaStore
task.max-memory=1GB
discovery-server.enabled=true
discovery.uri=http://localhost:8080
exception is
2013-11-11T11:11:39.582-0800 ERROR main com.facebook.presto.server.PrestoServer Guice creation errors:
1) Error: Constraint violation with property prefix '': environment is malformed (for class io.airlift.node.NodeConfig)
at io.airlift.node.NodeModule.configure(NodeModule.java:34)
1 error
com.google.inject.CreationException: Guice creation errors:
1) Error: Constraint violation with property prefix '': environment is malformed (for class io.airlift.node.NodeConfig)
at io.airlift.node.NodeModule.configure(NodeModule.java:34)
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435) ~[guice-3.0.jar:na]
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154) ~[guice-3.0.jar:na]
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106) ~[guice-3.0.jar:na]
at com.google.inject.Guice.createInjector(Guice.java:95) ~[guice-3.0.jar:na]
at io.airlift.bootstrap.Bootstrap.initialize(Bootstrap.java:277) ~[bootstrap-0.84.jar:0.84]
at com.facebook.presto.server.PrestoServer.run(PrestoServer.java:137) ~[presto-server-0.52.jar:0.52]
at com.facebook.presto.server.PrestoServer.main(PrestoServer.java:50) ~[presto-server-0.52.jar:0.52]
Apologies for the very unfriendly and unhelpful error message. This is on our list of things to fix.
The error "environment is malformed" means that the node.environment property (in the node.properties file) is set but fails to match the following regular expression: [a-z0-9][_a-z0-9]*. In other words, it must be lowercase, start with an alphanumeric character, and have only alphanumeric or underscores in subsequent characters.
Try a simpler name like test or production.
Not that it's of consolation, but I am getting something similar on Centos:
1) Error: Constraint violation with property prefix '': environment
may not be null (for class io.airlift.node.NodeConfig) at
io.airlift.node.NodeModule.configure(NodeModule.java:34)
1 error com.google.inject.CreationException: Guice creation errors:

Resources