Mockito ambiguous method call: any() matches class and object - unit-testing

I need to mock this code
rabbitTemplate.convertAndSend(message, m -> {
m.getMessageProperties().getHeaders().put("type", HEADER);
return m;
});
I my test I write
doNothing().when(rabbitTemplate).convertAndSend(anyObject(), any(MessagePostProcessor.class));
There is an error
Any ideas about workaround?

Instead of using anyObject(). Try specifying the type.
doNothing().when(rabbitTemplate).convertAndSend(Mockito.any(String.class), any(MessagePostProcessor.class));

Related

How to mock kotlin object for unit testing

I have a sample method(which I need to write test case) as given below,
fun setName(val auxName:String) {
val configUrl = getConfig(auxName)
}
I want to mock the getConfig method and return a specific string value.
getConfig is a method in a Kotlin Object as below,
object Configuration{
fun getConfig(auxName:String){
....
}
}
Below is the test that I tried
#Test
fun setTest()
{
val testname="test"
val testObject=Mockito.mock(Configuration::class.java)
doReturn("configTest").`when`(testObject).getConfig(Mockito.anyString())
setName(testname)
}
I am not getting any error but the method getConfig is not mocked. The actual implementation is executed. I tried using Powermockito also. Please help me with this
the problem is probably with singleton object, you can try this answer: https://stackoverflow.com/a/37978020/3703819

Using Kotlin and Mockito to mock functions taking vararg parameters

I'm having trouble utilizing Powermock, Mockito, and Kotlin to mock up a return value when
the function signature that needs overriden contains varargs.
Function to mock/override:
public BoundStatement bind(Object... values);
Test File:
class DbSessionTest {
var preparedStatement: PreparedStatement? = null
#Mock
var boundStatement: BoundStatement? = null
#Before
fun setup() {
initMocks(this)
whenever(preparedStatement?.bind(any())).thenReturn(boundStatement)
// Also, have tried it like this
whenever(preparedStatement?.bind(any<Array<Any>>())).thenReturn(boundStatement)
}
}
The fuction in the actual code is called like so:
internal fun createBoundStatement(query: String, vararg params: Any): BoundStatement {
val preparedStatement = prepare(query)
val boundStatement = preparedStatement.bind(*params)
return boundStatement
}
When I step through and the varargs are dereferenced, it turns into an Object[].
When the object array contains all the same type, everything works fine, but when
it contains type String and type Integer, the mock fails to happen and null is
returned.
Note: I have also included com.nhaarman.mockito_kotlin.any package for their specific
any function and anyVararg, but that did not work either.
What is the proper way to mock this so that no matter the type in the Object array,
it will always return the mocked value?
Nicholas Hauschild's answer make me think about removing powermock and just going with regular mockito and junit separately and that worked. Removing powermock and Upgrading mockito to v2.18.3 fixed the issue.

Can I mock a generic (template) private method using MockK in Kotlin?

I would like to mock the following function:
private fun <T> updateItemInDb(id: Long, column: String, data: T)
which is invoked in the following way by my class:
updateItemInDb(it, DB_POS, i), where it is a Long, DB_POS is String and i is an Int.
I want the function to just run without doing anything. I tried the following in my unit test:
1) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } just Runs
This gives me a type mismatch error: required MockKStubScope<Unit>, found MockKStubScope<Any?>
2) every { adapter["updateItemInDb"](any<Long>(), any<String>(), any<Int>()) } answers { }
This fails at runtime with io.mockk.MockKException: can't find function updateItemInDb(-1078155520644112829, -d008fa83c4f49c0, 843241211) for dynamic call
Now yes. Generic private functions was fixed since 1.7.16

Mocking extension function in Kotlin

How to mock Kotlin extension function using Mockito or PowerMock in tests? Since they are resolved statically should they be tested as static method calls or as non static?
I think MockK can help you.
It supports mocking extension functions too.
You can use it to mock object-wide extensions:
data class Obj(val value: Int)
class Ext {
fun Obj.extensionFunc() = value + 5
}
with(mockk<Ext>()) {
every {
Obj(5).extensionFunc()
} returns 11
assertEquals(11, Obj(5).extensionFunc())
verify {
Obj(5).extensionFunc()
}
}
If you extension is a module-wide, meaning that it is declared in a file (not inside class), you should mock it in this way:
data class Obj(val value: Int)
// declared in File.kt ("pkg" package)
fun Obj.extensionFunc() = value + 5
mockkStatic("pkg.FileKt")
every {
Obj(5).extensionFunc()
} returns 11
assertEquals(11, Obj(5).extensionFunc())
verify {
Obj(5).extensionFunc()
}
By adding mockkStatic("pkg.FileKt") line with the name of a package and file where extension is declared (pkg.File.kt in the example).
More info can be found here: web site and github
First of all, Mockito knows nothing Kotlin specific language constructs. In the end, Mockito will have a look into the byte code. Mockito is only able to understand what it finds there and what looks like a Java language construct.
Meaning: to be really sure, you might want to use javap to deassemble the compiled classfiles to identify the exact names/signatures of the methods you want to mock.
And obviously: when that method is static, you have to user PowerMock, or JMockit; if not, you should prefer to with Mockito.
From a java point of view, you simply avoid mocking static stuff; but of course, things get really interesting, now that different languages with different ideas/concepts come together.
Instance extension functions can be stubbed and verified like this with the help of mockito-kotlin:
data class Bar(thing: Int)
class Foo {
fun Bar.bla(anotherThing: Int): Int { ... }
}
val bar = Bar(thing = 1)
val foo = mock<Foo>()
with(foo) {
whenever(any<Bar>().bla(any()).doReturn(3)
}
verify(foo).apply {
bar.bla(anotherThing = 2)
}
I use mockk library.
For extension file write java name, like this:
#file:JvmName(name = "ExtensionUtils")
package myproject.extension
...
And for fast codding I created file with different extension mocks:
object FastMock {
fun extension() = mockkStatic("myproject.extension.ExtensionUtils")
fun listExtension() = mockkStatic("myproject.extension.ListExtensionUtils")
}
In test call this:
FastMock.listExtension()
every { itemList.move(from, to) } returns Unit

using Moq and generic type boxing issue

I tried to make a generic helper method using Moq with nunit as described below
public void PrepareWebRequest<T>() where T : new()
{
httpCommunicator = new Mock<IHttpCommunicator>();
httpCommunicator.Setup(x => x.Post(It.IsAny<Object>(),
It.IsAny<string>())).Throws<T>();
service = new ApiClient(httpCommunicator.Object);
}
But this causes the following error:
The type 'T' cannot be used as type parameter 'TException' in the generic type or method 'Moq.Language.IThrows.Throws()'. There is no boxing conversion or type parameter conversion from 'T' to 'System.Exception'.
I know this can be refactored by not using the generic method on Moq, but would really like to know what I'm doing wrong.
Best regards
Rasmus
Problem was a missing Exception in the Where clause. Should be
public void PrepareWebRequest<T>() where T : Exception, new()
{
httpCommunicator = new Mock<IHttpCommunicator>();
httpCommunicator.Setup(x => x.Post(It.IsAny<Object>(),
It.IsAny<string>())).Throws<T>();
service = new ApiClient(httpCommunicator.Object);
}