6

I'm trying to get the verify method in Mockito to work. I have the following test:

@Test
public void testShouldFail()
{
    String string = mock(String.class);
    string.length();
    verify(string, times(100)).length();
}

This test should fail, but it passes. Does anybody know why? Am I using Mockito wrong?

Update

Here's another example that doesn't fail:

private interface Bar
{
    public void foo();
}

@Test
public void testShouldFail()
{
    Bar bar = mock(Bar.class);
    bar.foo();
    verify(bar, times(100)).foo();
}
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149

1 Answers1

7

Well, you should be careful about that: by default, you cannot mock final classes (like String). This is a known limitation of the framework.

Your example fails for me with the proper error message:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class java.lang.String
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
    at Test.testShouldFail(Test.java:6)
         ...

So I guess there might be some minor configuration problems in your project. Which IDE are you using? Which version of Mockito do you have? How do you run your tests?

You might try using some additional toolkit like PowerMock which helps you overcome this limitation. This framework can be used in conjuction with Mockito quite easily.

On the other hand, String is part of the java.lang package, and I assume there are some additional security verifications involved of those classes by the VM (ain't sure though). I'm not convinced that you can mock (i.e., manipulate the bytecode) of such a class (for instance, you get a compilation error if you try to put something in the java.* package). But this is just an assumption from my side.

Eel Lee
  • 3,513
  • 2
  • 31
  • 49
rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • Honestly, I just used `String` as a quick example. Still, it's good to know Mockito have that limitation. I've updated my question with another example that doesn't fail. I'm using Eclipse Indigo with Junit 4.8.2, Mockito 1.9.0 and PowerMock 1.4.11. – LandonSchropp Mar 04 '12 at 09:29
  • Could you please include the full testclass you made, including the import directives too? Are you sure you've added the required JAR files to your project? – rlegendi Mar 04 '12 at 10:05
  • Sorry for the late reply. It looks like you were right. I messed up my Mockito library when I added PowerMock. Please update your answer so I can check it off. Thanks for the help. – LandonSchropp Mar 07 '12 at 06:55
  • @helixed No prob. I edited your source code including the necessary import statements, etc. It is failing for me with the proper error message: `TooLittleActualInvocations: bar.foo(); Wanted 100 times, But was 1 time...` It must be some configuration issue. Please post some additional infos (how do you run tests, which IDE are you using, etc.), see my previous comment. – rlegendi Mar 07 '12 at 08:16
  • I meant to say I got it working now. The problem was how with PowerMock and how I had included the library in my project. If you update your answer I'll check it off (since you asked if I added the files correctly to my application.) – LandonSchropp Mar 07 '12 at 08:39
  • @helixed Oh, ok! Glad to hear. – rlegendi Mar 07 '12 at 08:58