I found a few answers for this but nothing is working for me.
Trying to mock Apache Shiro Subject interface login method which returns void.
Interface:
void login(AuthenticationToken token) throws AuthenticationException;
I have tried :
#1
val subject = mock[Subject]
doNothing().when(subject).login(new UsernamePasswordToken())
#2
val subject = mock[Subject]
doNothing().when(subject).login(any[UsernamePasswordToken])
#3
when(subject.login(any[UsernamePasswordToken])).thenAnswer(new Answer[Void]() {
override def answer(invocation: InvocationOnMock): Void = {
null:Void
}
I keep getting NullPointerException on login. My goal is test around the login method and test some success cases along with some failure cases where this method throws an exception.
Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.
ScalaMock is a native, open-source Scala mocking framework written by Paul Butcher that allows you to mock objects and functions.
The default behavior in Mockito is to return nothing if you don't "stub" a particular method. No need to force a void() function to doNothing()
since it's doing nothing by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With