Using mockito.verify to ignore one of the parameters - mockito

I want to skip checking one of the parameters in a verify call. So for:
def allowMockitoVerify=Mockito.verify(msg,atLeastOnce()).handle(1st param,,3rd param)
I want to skip checking for the second parameter. How can I do that?

Unfortunately Mockito wont allow you to mix and match raw values and matchers (e.g. String and Matchers.any())
However you can use the eq() Matcher to match against a specific value, for example
Mockito.verify(msg, atLeastOnce())
.handle(eq("someValue"), any(Thing.class), eq("anotherValue"));
Thanks to this post for a good example of this
Mockito: InvalidUseOfMatchersException

You can try Mockito.any(), which basically means we are not interested in this parameter.

I'm using Mockito 3.9.0 and because you cannot mix matchers with expected values, i.e, you can't verify that the first argument is a specific string like test-profile and the second is anything, so you need to convert all to matchers, so you can't do something like:
verify(userAuthorizationService).authorizeRequest("test-profile", any());
Instead, you need to convert values into matchers, like:
verify(userAuthorizationService).authorizeRequest(matches("test-profile"), any());
Note the matches and any are static imports from org.mockito.ArgumentMatchers

Related

Call groovy script dynamically in Apache Camel using doTry-doCatch

I'm building a route which calls a groovy script whose path is dynamically computed and, if the script can't be found, defaults to a generic, static script:
.doTry()
.toD("language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
The problem is that the exchange property consumerType is not resolved since the uri string parameter of toD is evaluated using groovy and not simple.
MultipleCompilationErrorsException -> startup failed:
Script_09b4150584d9e2c979353feee06897b5.groovy: 1: Unexpected input: 'scripts/${exchangeProperty.consumerType}' # line 1, column 20.
resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy
^
1 error
How can I obtain the desired behavior?
According to the error shown there, it seems Camel is not able to resolve the string you provided in the toD().
By default, the expression you pass to a dynamic to is evaluated as Simple language but, as described in To Dynamic Camel documentation, you can specify other languages for the dynamic evaluation.
In your case, you are trying to evaluate the endpoint with groovy language but then you're using Simple language to substitute a piece of the name of the script.
One solution I've found (yet not the best) would be to specify the language for the interpretation of the string as simple and then use language:groovy to specify the endpoint that will need to be called.
You could write something like this:
.doTry()
.toD("language:simple:language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
It seems to work, but I hope someone else comes up with a better solution.

Can I mix one matcher and 1 exact value in mockito arguments

If I create a mock like this
when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestEnv.mockDistributedTransaction,ArgumentMatchers.any[ExternalUserProfileKeys]))
.thenReturn(Right(servicesTestEnv.externalUserProfile))
I get error
Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at UnitSpecs.ServiceSpecs.UserTransactionDatabaseServiceSpecs.$anonfun$new$17(UserTransactionDatabaseServiceSpecs.scala:170)
But if I replace both with value
when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestEnv.mockDistributedTransaction,keys))
.thenReturn(Right(servicesTestEnv.externalUserProfile))
or
when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(ArgumentMatchers.any[DistributedTransaction],ArgumentMatchers.any[ExternalUserProfileKeys]))
.thenReturn(Right(servicesTestEnv.externalUserProfile))
then I don't get the error
Is this a rule in Mockito that all argument either need to matchers or values?
Mixing of matchers and raw values is not supported by Mockito at the moment and mocking has to be done fully one way (matchers) or the other (concrete values), just like you've shown in your question.
There's a discussion on Mockito GitHub page regarding mixed argument mocking, but it's been pretty much dead for two years.

How to verify kotlin varargs function using mockito

I have a kotlin function of this form in an interface:
fun foo(bar: String, vararg baz: Pair<String, ByteArray>):Boolean
Using Mockito to mock this interface, how do I verify that this function was called with no Pairs?
It doesn't work leaving the second matcher off, because then Mockito complains that it needs two matchers.
Using any any*() matcher, including anyVararg(), fails because of typing.
A non-answer to give some inspiration:
Keep in mind that Mockito doesn't know or care what you are writing in some Kotlin source code file.
Mockito only deals with the compiled byte code. In other words: Mockito looks into the final classfile; created by the kotlin compiler.
Thus: your first stop should be javap to disassemble the class file that contains that method definition. You check the signature of the method there; and that should tell you how to specify correct argument matchers to Mockito.
And just another idea: java varargs translate arrays. So "no" args means: an empty array. So you probably want to match specifically on something like empty array of Pairs.

Using default function parameters on native

A haxe function has some parameters whose default values I'd like to use, so I don't need to import anything (they're basic types underneath). If they were last in the parameter order, I could get away with just not including them. But they're first, before some defaults I do want to override.
I'm not allowed to null them on native. _ doesn't compile (I don't think it's meant for this context.) Am I forced to import and copy the defaults in verbatim, or is there another way?
I tried .bind(_, ...)() but that gives Usage of _ is not supported for optional non-nullable arguments.
That error comes from the argument having a non-nullable type (Int, Float or Bool on a static target). If this function is part of your code and not some library, you could just make it nullable with Null<T> or ?.
As long as the arguments are nullable, Haxe also allows you to simply skip them if they are distuingishable (i.e. the type of the value passed must be different from the one(s) you want to skip). This means you don't have to use bind() or explicitly pass null. See the fourth example on the manual's Optional Arguments page.
If making the arguments nullable isn't an option for you in this particular case, you're probably going to have to copy the defaults (although I'm sure it's possible to come up with a clever macro solution for this).

SSRS - How to get a part of a string

I have a parameter called Analyst group in this format :
[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]
I want to pass this parameter to another report, to filter data. Its a multi value parameter. But the other report only accepts it in this format : [KanBan].[Analyst Group].&[Nl.Workplace.Foundation]
So im trying to isolate the "Nl.Workplace.Foundation", so i can do the following thing in the Go To Report parameter expression :="[KanBan].[Analyst Group].&["& --Isolated analyst group-- &"]" to create the desired format.
So what i need is to extract the part between .&[ and ]
But i really have no idea how to isolate that part of the string.
Found a solution! If i just use the Parameter.label instead of Parameter.value it automatically does what i want!
A different solution has been found, but I will still answer the initial question. It could help.
So what i need is to extract the part between .&[ and ]
You could use a regex.
This may not be the fastest way but it can handle most of the situations.
So let's assume you have a string containing:
[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]
And you want to get the following string:
Nl.Workplace.Foundation
Just use the following expression:
=System.Text.RegularExpressions.Regex.Match("[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]", "\.&\[(?<NWF>[^]]+)\]").Groups("NWF").Value
In the expression, replace the input string with your dynamic values, like for example:
=System.Text.RegularExpressions.Regex.Match(Fields!Dimension.Value & "." & Fields!AnalystGroup.Value, "\.&\[(?<NWF>[^]]+)\]").Groups("NWF").Value
I'm keeping the formula as simple as possible so that you can easily adapt it, with, say, handling the case where an input string will not have a match (with the above query it will return #Error).
You could do this by adding an IIF() or better, use a custom function that you can reuse in several places and will reduce the length of your expression.

Resources