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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:32:43+00:00 2026-06-14T18:32:43+00:00

I wish to achieve the following behavior. My class under test has a dependency

  • 0

I wish to achieve the following behavior.
My class under test has a dependency on some other class, I wish to mock this dependency with jMock. Most of the methods would return some standard values, but there is one method, where I wish to make a call to a stubbed implementation, I know I can call this method from the will(...) but I want the method to be called by the exact same parameters that were passed to the mocked method.

Test

@Test
public void MyTest(){
    Mockery context = new Mockery() {
        {
            setImposteriser(ClassImposteriser.INSTANCE);
        }
    };
    IDependency mockObject = context.mock(IDependency.class);
    Expectations exp = new Expectations() {         
        {
            allowing(mockObject).methodToInvoke(????);
            will(stubMethodToBeInvokedInstead(????));
        }       
    };      
}

Interface

public interface IDependency {
    public int methodToInvoke(int arg);
}

Method to be called instead

public int stubMethodToBeInvokedInstead(int arg){
    return arg;
}

So how do I capture the parameter that were passed to the method being mocked, so I could pass them to the stubbed method instead?

EDIT

Just to give another example, let’s say I wish to mock the INameSource dependency in the following (C#) code, to test the class Speaker

public class Speaker
{
  private readonly string firstName;
  private readonly string surname;
  private INameSource nameSource ;
 public Speaker(string firstName, string surname, INameSource nameSource)
  {
    this.firstName = firstName;
    this.surname = surname;
    this.nameSource = nameSource;
  }
  public string Introduce()
  {
    string name = nameSource.CreateName(firstName, surname);
    return string.Format("Hi, my name is {0}", name);
  }
}
public interface INameSource
{
  string CreateName(string firstName, string surname);
}

This is how it can be done in Rhino Mocks for C# I understand it can’t be as easy as this since delegates are missing in Java

  • 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-14T18:32:44+00:00Added an answer on June 14, 2026 at 6:32 pm

    The solution from Duncan works well, but there is even a simpler solution without resort to a custom matcher. Just use the Invocation argument that is passed to the CustomActions invoke method. At this argument you can call the getParameter(long i) method that gives you the value from the call.

    So instead of this

    return matcher.getLastValue();
    

    use this

    return (Integer) invocation.getParameter(0);
    

    Now you don’t need the StoringMatcher anymore: Duncans example looks now like this

    @RunWith(JMock.class)
    public class Example {
    
      private Mockery context = new JUnit4Mockery();
    
      @Test
      public void Test() {
    
        final IDependency mockObject = context.mock(IDependency.class);
    
        context.checking(new Expectations() {
          {
            // No custom matcher required here
            allowing(mockObject).methodToInvoke(with(any(Integer.class)));
    
            // The action will return the first argument of the method invocation.
            will(new CustomAction("returns first arg") {
              @Override
              public Object invoke(Invocation invocation) throws Throwable {
                return (Integer) invocation.getParameter(0);
              }
            });
          }
        });
    
        Integer test1 = 1;
        Integer test2 = 1;
    
        // Confirm the object passed to the mocked method is returned
        Assert.assertEquals((Object) test1, mockObject.methodToInvoke(test1));
        Assert.assertEquals((Object) test2, mockObject.methodToInvoke(test2));
      }
    
      public interface IDependency {
        public int methodToInvoke(int arg);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I have created two buttons in a gridview. I wish to achieve the following
Using JqueryMobile, I wish to achieve the following layout: Having nice decorations boxes to
I wish to use Java2D drawString to achieve the following looks. However, I have
I wish to create the following test in NUnit for the following scenario: we
I have the following php and wish to know how I can achieve that
I have started learning with Liferay 6.1 What I wish to achieve is: Displaying
I wish to do this using only XAML - How to add an additional
I wish I could figure this out. I need to produce a table with
I'm trying to achieve the following behaviour in html: user is presented with a

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.