Mockito check not crashing

I am trying to use a method verifyin Mockito. 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 will pass. Does anyone know why? Am I using Mockito incorrectly?

Update

Here is another example that fails:

private interface Bar
{
    public void foo();
}

@Test
public void testShouldFail()
{
    Bar bar = mock(Bar.class);
    bar.foo();
    verify(bar, times(100)).foo();
}
+5
source share
1 answer

Well, you have to be careful about this: by default, you cannot mock final classes (e.g. String). This is a known limitation in structure.

Your example is not suitable for me with the corresponding 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)
         ...

, , . IDE ? Mockito ? ?

, PowerMock, . Mockito .

, String java.lang, , , VM ( , ). , (.. -) (, , - java.*). .

+5

All Articles