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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:06:42+00:00 2026-06-03T17:06:42+00:00

I’m running into a problem with trying to set up a Mockery to test

  • 0

I’m running into a problem with trying to set up a Mockery to test for method invocation. I saw How to test protected methods of abstract class using JUnit and JMock and it’s essentially the same question, but I can’t make out what Chris is referring to in his answer. If I could please have someone explain it to me in a slightly different way or with more complete English. I’m using JUnit, JMock2, Infinitest (if that matters), and a standard event-based message system as follows:

public Interface MyEventListener {
    void handleMyEvent(MyEvent e);
}

public Class MyEvent extends EventObject {
    public MyEvent(Object source) {
        super(source);
    }
}

public Class MySource {
    protected List<MyEventListener> listeners = new ArrayList<MyEventListeners>();

    public void addListener(MyEventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(MyEventListener listener) {
        listeners.remove(listener);
    }

    // other code...

    protected void raiseEvent() {
        for (MyEventListener listener : listeners) {
            listener.handleMyEvent(new MyEvent(this));
        }
    }
}

Now here’s my test:

@Test
public void sourceShouldThrowEventOnEventOccurrence() {
    // set up
    Mockery context = new Mockery();
    MyEventListener listener = context.mock(MyEventListener.class);
    MySource source = new MySource();
    MyEvent event = new MyEvent(source);
    source.addListener(listener);

    // set expectations
    context.checking(new Expectations() {{
        oneOf(listener).handleMyEvent(event);
    }});

    // execute
    // do stuff to (theoretically) raise event

    // verify
    context.assertIsSatisfied();
}

I get an ExpectationError (unexpected invocation) on my protected method of raiseEvent() in MySource. How can I add to the expectation that it should call the protected method?

Also, a thought just occurred to me – but I’m still posting this message before I go explore it (in case someone else has the same question and my thought ends up being right or not). Could the error possibly be related to the fact that I have two new keywords in my code? I have new MyEvent(this) in MySource.raiseEvent() and then I have new MyEvent(source) in the test. This creates two different events in memory… Is the error related to the possibility that it expects the method to get one event, but receives the other?

*****EDIT

Actually, specifically, I get an ExpectationError (unexpected invocation) on the line of code that actually raises the events. (In my code above, the listener.handleMyEvent(new MyEvent(this)); line)

  • 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-03T17:06:44+00:00Added an answer on June 3, 2026 at 5:06 pm

    It turns out my suspicion that it was related to the fact that two different MyEvent instances were being created was correct. Unfortunately, it means the title to my question is misleading. After digging, I found out that my code worked fine once I changed my Expectations() to the following.

    @Test
    public void sourceShouldThrowEventOnEventOccurrence() {
        // set up
        Mockery context = new Mockery();
        MyEventListener listener = context.mock(MyEventListener.class);
        MySource source = new MySource();
        // MyEvent event = new MyEvent(source);  <-- NO LONGER NEEDED
        source.addListener(listener);
    
        // set expectations
        context.checking(new Expectations() {{
            oneOf(listener).handleMyEvent(with(any(MyEvent.class)));  // <-- with any
        }});
    
        // execute
        // do stuff to (theoretically) raise event
    
        // verify
        context.assertIsSatisfied();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.