### Question:
MemoryCassandraInstanceDao implements CassandraInstanceDao { @Override public void save(String ringName, CassandraInstance instance) { LOG.info("Saving instance: {}", instance); if (!rings.containsKey(ringName)) { rings.put(ringName, Maps.<Integer, CassandraInstance>newHashMap()); } rings.get(ringName).put(instance.getId(), instance); } MemoryCassandraInstanceDao(); @VisibleForTesting  MemoryCassandraInstanceDao(
      ConcurrentMap<String, Map<Integer, CassandraInstance>> rings); @Override ImmutableSet<CassandraInstance> findAll(String ring); @Override @Nullable CassandraInstance findById(String ring, int id); @Override void save(String ringName, CassandraInstance instance); @Override void delete(String ringName, CassandraInstance instance);  }### Answer:
@Test public void save_existingRing() { CassandraInstance instance = createMock(CassandraInstance.class); @SuppressWarnings("unchecked") Map<Integer, CassandraInstance> instanceMap = createMock(Map.class); expect(instance.getId()).andStubReturn(CASSANDRA_ID); expect(rings.containsKey(RING_NAME)).andStubReturn(true); expect(rings.get(RING_NAME)).andStubReturn(instanceMap); expect(instanceMap.put(CASSANDRA_ID, instance)).andStubReturn(null); replayAll(); dao.save(RING_NAME, instance); }
@SuppressWarnings("unchecked") @Test public void save_newRing() { CassandraInstance instance = createMock(CassandraInstance.class); expect(instance.getId()).andReturn(CASSANDRA_ID); expect(rings.containsKey(RING_NAME)).andReturn(false); Map<Integer, CassandraInstance> instanceMap = createMock(Map.class); expect(rings.get(RING_NAME)).andReturn(instanceMap); expect(rings.put(eq(RING_NAME), anyObject(Map.class))).andReturn(null); expect(instanceMap.put(CASSANDRA_ID, instance)).andReturn(null); replayAll(); dao.save(RING_NAME, instance); }