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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:51+00:00 2026-06-11T02:55:51+00:00

Possible Duplicate: Intercept object on method invocation with Mockito I have an class that

  • 0

Possible Duplicate:
Intercept object on method invocation with Mockito

I have an class that can mapp from one format to another. Since this is legacy code I don’t dare to rewrite it, it is basically a set of plugins so if I change one I might have to change all the other. It wasn’t developed with testing in mind.
So this is my problem.

interface Mapper {
    void handle(ClassA classA);
    void handle(ClassB classB);
}

public interface Publisher {
    public void publish(MappedClass mappedClass);
}

class MyMapper implements Mapper {

    private Publisher publisher;
    public void setPublisher(final Publisher publisher) {
        this.publisher = publisher;
    }

    public handle(ClassA classA) {
        final MappedClass mappedClass = // Map from ClassA to MappedClass
        publisher.publish(mappedClass);
    }

    public handle(ClassB classB) {
        final MappedClass mappedClass = // Map from ClassB to MappedClass
        publisher.publish(mappedClass);
    }
}

Okay. So depending on which class was “handled” MappedClass will be published with different values, and it is the values I want to verify (test). The problem is that I will get a test where I first have to write code that tests that the publish method is called,

private boolean wasCalled;

@Test
public void testClassAMapped() {
    wasCalled = false;
    final MyMapper myMapper = new MyMapper();
    myMapper.setPublisher(new Publisher() {
        public void publish(final MappedClass mappedClass) {
            wasCalled = true;
           // Code for verifying the fields in mappedClass
        });
    }
    final ClassA classA = // Create classA
    myMapper.handle(classA);
    assertTrue(wasCalled);
}

So first we create our mock Publisher which will first set the state of wasCalled to true so we know this method was ever called (this example is simplified so there is actually a dispatcher in the code… legacy code so I don’t want to change it), second I want to verify that MappedClass has the correct field values.
What I would like to know is if anyone knows a better way to test this? The wasCalled, and wasCalled check becomes more or less boilerplate code for many of my tests, but since I don’t want to add that much clutter (own hacks, test base classes, etc) I would like to know if there is a way to do this in Mockito, or EasyMock?

  • 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-11T02:55:53+00:00Added an answer on June 11, 2026 at 2:55 am

    Use an Mockito ArgumentCaptor

    @Test
    public void test(){
      Publisher publisher = Mockito.mock(Publisher.class);
      myMapper.setPublisher(publisher);
      ArgumentCaptor<MappedClass> captor = ArgumentCaptor.forClass(MappedClass.class);
    
      ....
      myMapper.handle(...);
      ...
    
      verify(publisher).publish(captor.capture());
      MappedClass passedValue = captor.getValue();
      // assert stuff here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ Static member method call on class instance Today I discovered that
Possible Duplicate: Can .NET intercept and change css files? I have configured IIS6 to
Possible Duplicate: Intercept calls to console.log in Chrome Can I extend the console object
Possible Duplicate: What do parentheses surrounding a JavaScript object/function/class declaration mean? Recently discussing with
Possible Duplicate: Intercept method call in Objective-C How to log all methods used in
Possible Duplicate: Operator[][] overload I've looked on the internet but can't find a definitive
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: <blink> tag in Internet Explorer Hai I have used blink tag to
Possible Duplicate: how can i validate a url in javascript using regular expression I
Possible Duplicate: Accessing Windows registry with PHP and DOTNET class Is there a way

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.