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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:13:45+00:00 2026-06-14T09:13:45+00:00

Is there the best way to make nice mock by using JMock? For example:

  • 0

Is there the best way to make nice mock by using JMock?

For example:

public interface Dependency {
    void someSetUp();
    void interactionUnderTest();
    void someCleaningAfterWork();
}

public class CUT {
    private Dependency dependency;

    public void methodUnderTest() {
        dependency.someSetUp();
        dependency.interactionUnderTest();
        dependency.someCleaningAfterWork();
    }

    public void setDepencency(Dependency dependency) {
        this.dependency = dependency;
    }
}

For Mockito the solution is simple:

@Test
public void mockitoExample() throws Exception {
    Dependency dependency = mock(Dependency.class);
    classUnderTest.setDepencency(dependency);
    classUnderTest.methodUnderTest();
    verify(dependency).interactionUnderTest();
}

But for JMock I’ve found only this solution:

@Test
public void jMockExample() throws Exception {
    JUnit4Mockery ctx = new JUnit4Mockery();
    final Dependency dependency = ctx.mock(Dependency.class);
    classUnderTest.setDepencency(dependency);
    ctx.checking(new Expectations() {{
        allowing(dependency).someSetUp();
        one(dependency).interactionUnderTest();
        allowing(dependency).someCleaningAfterWork();
    }});

    classUnderTest.methodUnderTest();
}

The problem is in these lines:

allowing(dependency).someSetUp();
and
allowing(dependency).someCleaningAfterWork();

If methodUnderTest changes, for example by calling another method from Dependency, I’ll have to manually modify the test in case of using JMock. Is there the way to avoid it?

P.S. Sorry about my English

  • 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-14T09:13:47+00:00Added an answer on June 14, 2026 at 9:13 am

    If you really want to, you can use allowing(dependency) which will allow everything on the object.

    There is a reason that we don’t support “nice” mocks, which is that we don’t want stuff happening arbitrarily in our code without driving it from the unit test. On the whole, we find that this sort of brittleness comes from weakness in our design, and we use the test feedback to help us discover that.

    In this example, I would be interested in the relationship, the “protocol” between the different dependency methods. I would want to isolate that so that it’s implemented once, which means I would only need to test it in once place, so a “nice” mock would not be as useful.

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

Sidebar

Related Questions

What is the best way (or, is there a way) to implement a dependency
What's the best way to make an element unselectable using jQuery? I know you
is there a best way to deploy to the GAC? is dragging the dll
Is there a best way to edit CSS? I'm looking for a designer tool.
is there any best practice way to replace a part of the default template.
Is there a standard/best way to space items in a WPF ListBox , such
Is there a best (see below) way to append two byte arrays in C#?
What is best way to create a blinking image animation in wp7? Is there
What is the best way to extract each field from each line where there
What is the best way to compute the speed/velocity of a swipe? Is there

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.