2

Im getting the following error, when I try to mock overloaded methods by passing casted values .

For example in order to mock ABCClass.logWarn(Logger log,String , String description, Throwable e);

Im doing

`ABCClass.logWarn(null,WarningString, description, (Throwable)null); 
...\\ The rest of the methods are standard...
verify(event).setStatus((Throwable)null);//**Line 76**

But when I run my test cases Im getting the following error

  ABCClassTest.testLogWarn:76 
    Wanted but not invoked:
    MockEvent.setStatus(null);
    -> at com.path.ABCClassTest.testLogWarn(ABCClassTest.java:76)

However, there were other interactions with this mock:.....

Why is setStatus(null) expected to be called, even after specifically calling the setStatus((Throwable)null);?

Additional Detail

Definition of logWarn

private static void logWarn(String eventType, String eventName, String errMsg, Throwable e) {

        AnEvent event = AnEventFactory.create(eventType);
        event.setName(eventName);
        if(e!=null)
            event.setStatus(e);//so this is never called if the throwable is null.
    //How do I modify the verify behavior?
        /*
                   Bleh */


        event.completed();
    }
seeker
  • 6,841
  • 24
  • 64
  • 100

2 Answers2

1

Casting doesn't change the object that a variable refers to. It just makes the compiler not complain when you use the variable in a way that doesn't match its type. So you really are passing null into setStatus following your verify.

Of course, if you're asking why setStatus isn't actually called by the code that you're testing, you'd need to post it before anyone can tell you.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I think your answer makes sense. Ive added the code about the `logWarn` method, that actually does not call the `setStatus` if the argument passed is Null. – seeker Aug 13 '13 at 20:50
  • @DavidWallace.Ive added more detail to the question. Any thoughts? – seeker Aug 13 '13 at 21:06
  • 1
    So I don't understand your question. Your code clearly doesn't call `setStatus` when `e` is null. You've got a `verify` that asserts that it **does** get called; and naturally, that `verify` fails. What do you feel isn't working right? – Dawood ibn Kareem Aug 13 '13 at 21:23
  • Sorry for the noise. I found exactly what I was looking for here. http://stackoverflow.com/questions/12862659/how-to-verify-that-a-specific-method-was-not-called-using-mockito – seeker Aug 13 '13 at 21:36
  • 1
    OK, your question never said that you were looking to verify that your method was NOT being called. I have voted to close your question as "unclear" - anyone who has your precise problem would probably be better off not to find this question. – Dawood ibn Kareem Aug 13 '13 at 21:44
  • Actually EVEN if I called it, the `Status` would never be set because of the `if (e!=null)` clause around setting it.And since I was passing in a `null` I couldnt expect it to be called. – seeker Aug 13 '13 at 21:47
-1

Being new to Mockito I didnt quite realise what I was looking for. But this is precisely what I wanted. Hope this helps anyone else stuck with a similar problem.

Community
  • 1
  • 1
seeker
  • 6,841
  • 24
  • 64
  • 100