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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:47:00+00:00 2026-06-16T00:47:00+00:00

I changed some method somewhere in our code which shouldn’t have caused any weird

  • 0

I changed some method somewhere in our code which shouldn’t have caused any weird test failures, but JMock seems to think otherwise.

I boiled the issue down to the minimal amount of cruft, which looks something like this:

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;

public class TestMocking {

    @Test
    public void test() {
        Mockery mockery = new Mockery() {{
            setImposteriser(ClassImposteriser.INSTANCE);
        }};
        final Owner owner = mockery.mock(Owner.class);
        final RealThing thing = mockery.mock(RealThing.class, "thing");

        mockery.checking(new Expectations() {{
            oneOf(owner).getThing(); will(returnValue(thing));
            oneOf(thing).method(); will(returnValue(Collections.emptyList()));
        }});

        owner.getThing().method();

        mockery.assertIsSatisfied();
    }

    public static interface Owner {
        BaseThing getThing();
    }

    public static interface BaseThing {
        Collection<?> method();
    }

    public static interface RealThing extends BaseThing {
        List<?> method();
    }
}

(Edit: This now uses a ClassImposteriser even though there are no classes anymore, because I wanted to demonstrate that you could run the exact same code without that imposteriser and the test would pass.)

The result of running this:

unexpected invocation: thing.method()
expectations:
  expected once, already invoked 1 time: owner.getThing(); returns <thing>
  expected once, never invoked: thing.method(); returns <[]>
what happened before this:
  owner.getThing()

So there you go, “unexpected” thing.method() when the expected thing.method() was never called. I have previously seen this occur when multi-threaded classes are under test against mock objects, but this time it’s all happening in a single thread. It’s like JMock is somehow returning a different mock object from the first method call, even though I mocked no such object.

If I remove the overridden method which is returning a more specific type then it goes away, but I obviously can’t do that. Likewise, if I remove the use of ClassImposteriser, the problem goes away, but one of the objects I’m mocking in the real test is someone else’s class. I guess I could try using two Mockery instances in the one test, but aside from that I’m out of ideas.

  • 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-16T00:47:01+00:00Added an answer on June 16, 2026 at 12:47 am

    Hiding class (static) methods doesn’t work quite the same as Overriding instance methods. To prove that JMock is not to blame here, try this:

    public class test3 {
    public static void main(String[] args) {
        Owner owner = new Owner();
        owner.getThing().method(); //Like how you execute your test
        RealThing thing = new RealThing();
        thing.method(); //Similar to your mock.
    }
    
    private static class Owner {
        private BaseThing thing = new RealThing();
    
        public BaseThing getThing() {
            return thing;
        }
    }
    
    private static class BaseThing {
        public static void method() {
            System.out.println("Basething!");
        }
    }
    
    private static class RealThing extends BaseThing {
        public static void method() {
            System.out.println("Realthing!");
        }
    }
    }
    

    Note that the two calls to method() print different things! Both are instances of RealThing, but they call different methods. The static method called depends on whether it is called from the subcalss or the superclass. In the first call above, method is declared as a BaseClass, so BaseClass.method() is called, even though it is an instance of RealClass. The second call to method() is declared as a RealClass, so RealClass.method() is invoked.

    So, the results from JMock are valid. The method() called was not the same as the one you set up an expectation for.

    Don’t feel great about my explanation of this. Please do read up on it here: http://docs.oracle.com/javase/tutorial/java/IandI/override.html


    The fix (favoring BaseThing.method()), change:

    final RealThing thing = mockery.mock(RealThing.class, "thing");
    

    To:

    final BaseThing thing = mockery.mock(RealThing.class, "thing");
    

    Or if you prefer to use RealThing.method(), change:

    owner.getThing().method()
    

    To:

    RealThing thing = (RealThing)owner.getThing();
    thing.method();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently refactoring some Javascript code we have and amongst other things I've changed
I am creating a Java unit test to test some code I recently changed.
The other day this code was working. I changed some things and re-ran it
I have a large PHP application. After I changed some settings I get a
I wrote some code today and It was changed by another developer who said
While cleaning some code today written by someone else, I changed the access modifier
Was looking at some code in our codebase and I'm unable to understand how/why
I have wired experience with reflections. At first some sample code: public abstract class
I have a class that does some time-consuming calculations. I'm trying to performance test
I have some data that I want to store somewhere in my Rails app

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.