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
Related
I am trying mockito to mock a getValue function that consumes a java class as parameter.
To simplify, i did the following test:
#Test
fun test1() {
val map = HashMap<String,Any>()
val v:Long = 1L
map["K"]= v
println(map["K"]!!::class.java) //prints class java.lang.Long
println(Long::class.java) //prints long
val dss = Mockito.mock(DataSnapshot::class.java)
Mockito.`when`(dss.getValue( map["K"]!!::java.class))
.thenReturn( map["K"]!!)
//production code uses calls the function like this but it fails to get the value. Returns null;
Assert.assertEquals( map["K"],dss.getValue(Long::class.java))
}
As the prints show, the type in map["K"]!!::class.java is different from Long::class.java.
If i mock the method using the type inline it works:
Mockito.`when`(dss.getValue( Long::class.java))
.thenReturn( map["K"]!!)
How can i mock the method in a way that the type parameter hasn't to be determined by a long switch logic?
Some insides in kotlin and java types could help.
If you're asserting against java.lang.Long, use Long?::class.java. Long::class.java will give you the primitive long on the JVM. Nullable Long instead maps to the boxed version - java.long.Long.
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.
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));
I tried for searching a lot regarding this but did not find any helpful information. And I'm stuck in below situation and seeking for help.
Below is my method which I want to test:
public void MethodToTest()
{
this._app = this.GetApp(out this._pId);
}
//here beelow method which is from the same class and private
private Microsoft.Office.Interop.Word.Application GetApp(out int processId)
{
//some code which returns Application
}
Now I dont want to execute this method instead I want to shim this method
using
ShimMyclassName.Allinstances.set_GetAppInt32Out = () => my expected output
But compiler is throwing error:
cannot assign to set_GetAppInt32Out because it's a method group.
So I tried to changed it to:
ShimMyclassName.Allinstances.set_GetAppInt32Out( here it is expecting some out delegate like (OutFunc<WordApplication, int, Microsoft.office.Interop.Word.Application> Value)
In ShimMyClass below is the signature:
public void set_GetAppInt32Out(object value);
So I'm stuck here. How can I pass the value and How I can Break the private method call and expect it to return my expected output instead of executing the original private method?
I'm using Google Mock (gMock) for the first time. Given the following code snippet:
class LinkSignals
{
public:
virtual ~LinkSignals() { }
virtual void onLink(std::string) = 0;
virtual void onUnLink() = 0;
};
class MockLinkSignals : public LinkSignals
{
public:
MOCK_METHOD1(onLink, void(std::string));
MOCK_METHOD0(onUnLink, void());
};
MockLinkSignals mock_signals;
When I execute some test code that causes EXPECT_CALL(mock_signals, onLink(_)) to be run how can I inspect the argument to onLink()?
You would normally use either existing gmock matchers or define your own to check the arguments passed to the mock method.
For example, using the default Eq equality matcher:
EXPECT_CALL(mock_signals, onLink("value_I_expect"))
Or check for sub string say:
EXPECT_CALL(mock_signals, onLink(HasSubstr("contains_this")))
The gmock documentation provides details of the standard matchers that are available, and also describes how to make custom matchers, for example for an integer argument type:
MATCHER(IsEven, "") { return (arg % 2) == 0; }
It is possible to capture an argument to a variable by attaching an action to the expectation, although this can have limited use in the scope of the expectation:
EXPECT_CALL(mock_signals, onLink(_)).WillOnce(SaveArg<0>(pointer))
I'd suggest studying the various matchers and actions available before choosing the best approach for your particular case.