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

  • Home
  • SEARCH
  • 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 8011423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:00:46+00:00 2026-06-04T19:00:46+00:00

I have a dependency with a method that takes a Map as an argument.

  • 0

I have a dependency with a method that takes a Map as an argument.

public interface Service {
    void doSomething(Map<String, String> map);
}

I’d like to write an assertion that this dependency is called with appropriate map contents. Something like this:

@RunWith(JMock.class)
public class MainTest {
    private Mockery context = new Mockery();
    private Service service = context.mock(Service.class);
    private Main main = new Main(service);

    @Test
    public void test() {
        context.checking(new Expectations(){{
            oneOf(service).doSomething(with(hasEntry("test", "test")));
        }});
        main.run();
    }
}

Unfortunately, this fails to compile, since hasEntry has wildcards in map generic parameters:

public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K, ? extends V>> hasEntry(K key, V value);

Is there any way to write a JMock expectaion for map contents?

  • 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-04T19:00:47+00:00Added an answer on June 4, 2026 at 7:00 pm

    There isn’t a good answer to this as we hit the limits of Java generics. There’s a tension between the generics we need for jMock and what we need for assertThat()

    I tend to add a helper method, with an expressive name, to force the types.

    @Test public void test() {
      context.checking(new Expectations(){{
        oneOf(service).doSomething(with(mapIncluding("test", "test")));
      }});
    
      main.run();
    }
    
    @SuppressWarnings({"unchecked", "rawtypes"})
    private Matcher<Map<String, String>> mapIncluding(String key, String value) {
       return (Matcher)Matchers.hasEntry(key, value);
    };
    

    Yes, this is pig-ugly. I can only apologise that this is the best we appear to be able to do. That said, it’s rare that I have to go as far as turning off the types, I can give it a name that’s meaningful in the domain, and I’ve localised the unchecking to the helper method.

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

Sidebar

Related Questions

I have a wcf service that takes in an IRepository IRepository irepo; public SomeService(IRepository
I have a dependency injection container setup in my application and I'm thinking that
I have a string dependency property (SearchText), when updated, needs to update a collection
I have the following dependency property inside a class: class FooHolder { public static
I have the following dependency property that works fine but it does not auto
Suppose I have an interface that supports a few potential operations: interface Frobnicator {
I have a class like this: public FooRepo : IFooRepo { public FooRepo(IDbContextFactory factory)
I have the following query method in my ActiveRecord model: def self.tagged_with( string )
In project i have reference to Microsoft.Office.Word.Server and in code i have method that
When testing a class that uses a Moq dependency, I'd like the class to

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.