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 7744725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:50:11+00:00 2026-06-01T09:50:11+00:00

I get different output when mocking a class once and when mocking a class

  • 0

I get different output when mocking a class once and when mocking a class two times in a test. I know that @Mocked mocks all instances of a class but am not sure why mocking more than once affects the output of newly created objects.
Is this behaviour expected?

Test 1. Prints 10:

 @Test
 public void jmockitTest1(@Mocked final Date d1)
 {
       new NonStrictExpectations()
       {{
            d1.getTime(); returns(10L);
       }};

       System.out.println( d1.getTime() );  // prints 10
       System.out.println( new Date().getTime() ); // prints 10 !
 }

Test 2 with second mocked Date. Prints 0:

 @Test
 public void jmockitTest2(@Mocked final Date d1, @Mocked final Date d2)
 {
       new NonStrictExpectations()
       {{
            d1.getTime(); returns(10L);
       }};

       System.out.println( d1.getTime() );  // prints 10
       System.out.println( new Date().getTime() ); // prints 0 !
 }
  • 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-06-01T09:50:12+00:00Added an answer on June 1, 2026 at 9:50 am

    The second test, with Date mocked twice, gets “on-instance” matching by default. So, it’s as if the expectation was recorded as onInstance(d1).getTime();.

    This difference in mocking behavior is activated automatically merely as a convenience. The only reason to declare multiple mock fields/parameters of the same type in the same test is to have different results from calls to different mocked instances; the automatic “on-instance” matching avoids the need to use onInstance(mock) on each of those instances.

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

Sidebar

Related Questions

std::cout << (DWORD)test; If I compile and run this I get different output values
Two Questions: Will I get different sequences of numbers for every seed I put
Can someone explain why I get different results from these two statements? I thought
I must get two different client application talk without any kind of broker or
Can someone please let me know how to get the different segments of the
Here when I get file size using stat() it gives different output, why does
Apparently, a Perl script I have results in two different output files depending on
When I run my application I get different output compare to my colleague's. The
I'm seeing two different behaviors based on the CLR that my application is being
Why do I get after the autodie a different output? #!/usr/bin/env perl use warnings;

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.