How to mock creation of two different static ConcurrentHashMaps?

789 views
Skip to first unread message

KARR, DAVID

unread,
Jul 22, 2013, 2:33:57 PM7/22/13
to powe...@googlegroups.com
I have a block of code in a CUT (class under test) that looks like this:

private static ConcurrentHashMap<String, Method> cachedMethodsMap =
new ConcurrentHashMap<String, Method>();

private static ConcurrentHashMap<String, Field> cachedFieldsMap =
new ConcurrentHashMap<String, Field>();

I need to mock the creation of these maps so I can write assertions on them.

I noticed the information at <http://code.google.com/p/powermock/wiki/MockitoUsage13>, but that doesn't appear to go far enough.

It's possible I could make it use the same mocked map for both, but that's not what I want.

It's possible that these being static will be an additional complication.

Matt Lachman

unread,
Jul 22, 2013, 2:38:53 PM7/22/13
to powe...@googlegroups.com
Do you need to assert that the maps contain what you want? In that case, you wouldn't need to mock the creation of the maps, you'd just need to be able to get at them somehow.

Since they are private, you can use Whitebox.getField(CUT.class, "cachedMethodsMap") to get the first map for instance. Then assert on what you'd expect to be in there.




--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.



KARR, DAVID

unread,
Jul 22, 2013, 3:37:02 PM7/22/13
to powe...@googlegroups.com

Thanks, that did it.

Reply all
Reply to author
Forward
0 new messages