Reputation: 355
Is there any way to cast java.util.map(HashMap) to HazelCast IMap ?
Map<String, User> map = ....;
IMap<String, User> imap;
Thanks
Upvotes: 3
Views: 4328
Reputation: 2039
Quote from the official Hazelcast documentation:
Let's say you want to test if two members have the same size of a map.
@Test
public void testTwoMemberMapSizes() {
// start the first member
HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
// get the map and put 1000 entries
Map map1 = h1.getMap( "testmap" );
for ( int i = 0; i < 1000; i++ ) {
map1.put( i, "value" + i );
}
// check the map size
assertEquals( 1000, map1.size() );
// start the second member
HazelcastInstance h2 = Hazelcast.newHazelcastInstance();
// get the same map from the second member
Map map2 = h2.getMap( "testmap" );
// check the size of map2
assertEquals( 1000, map2.size() );
// check the size of map1 again
assertEquals( 1000, map1.size() );
}
p.s. please don't write like that, use given when then / arrange act assert
Upvotes: 3
Reputation: 721
I know it is old but here is my solution:
Add an extra dependency to the project:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<classifier>tests</classifier>
<version>${hazelcast.version}</version>
<scope>test</scope>
</dependency>
In your unit test you will have these fields:
@Mock
private HazelcastInstance hazelcastInstance;
private TestHazelcastInstanceFactory hazelcastFactory = new TestHazelcastInstanceFactory();
And finally, in your test (assuming you are using Mockito):
// mock the hazelcast map
IMap<Object, Object> mockedMap = hazelcastFactory.newHazelcastInstance().getMap("doesntmatter");
mockedMap.put("some-key", someObject);
when(hazelcastInstance.getMap("testMap")).thenReturn(mockedMap);
Upvotes: 3
Reputation: 5456
Mehrdad,
Hazelcast IMap
implements Map
and ConcurrentMap
interfaces.
In the following case, objects map2
and map
will point to the same distributed object (even more, map2
and map
would point to the same proxy object).
Map<Object, Object> map2 = hazelcastInstance.getMap("test");
IMap<Object, Object> map = hazelcastInstance.getMap("test");
With Map
interface you're limited with "standard" methods. IMap
provides extensions like submitting EntryProcessors, adding Event listeners.
I hope it does make sense. Let me know if you have any questions.
Thank you
Upvotes: 2