Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:46:13+00:00 2026-05-30T19:46:13+00:00

I’m trying to get the verify method in Mockito to work. I have the

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:46:15+00:00Added an answer on May 30, 2026 at 7:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.