### Question:
DefaultPortalPreferenceService implements PortalPreferenceService { @Override @Transactional public void savePreference(String key, String value) { List<String> values = new ArrayList<String>(); values.add(value); this.savePreference(key, values); } @Autowired  DefaultPortalPreferenceService(PortalPreferenceRepository repository, RaveEventManager manager); @Override Map<String, PortalPreference> getPreferencesAsMap(); @Override PortalPreference getPreference(String key); @Override SearchResult<PortalPreference> getAll(); @Override SearchResult<PortalPreference> getLimitedList(int offset, int pageSize); @Override @Transactional void savePreference(String key, String value); @Override @Transactional void savePreference(String key, List<String> values); @Override @Transactional void savePreference(PortalPreference preference);  }### Answer:
@Test public void testSaveKeyValue_new() { final String key = "foo"; final String value = "bar"; PortalPreference fooBar = new PortalPreferenceImpl(key, value); PortalPreferenceImpl fooBarSaved = new PortalPreferenceImpl(key, value); expect(repository.getByKey(key)).andReturn(null).once(); expect(repository.save(fooBar)).andReturn(fooBarSaved).once(); replay(repository); service.savePreference(key, value); verify(repository); }
@Test public void testSaveKeyValue_existing() { final String key = "foo"; final String value = "bar"; final String newValue = "baz"; PortalPreferenceImpl fooBar = new PortalPreferenceImpl(key, value); PortalPreferenceImpl fooBarSaved = new PortalPreferenceImpl(key, newValue); expect(repository.getByKey(key)).andReturn(fooBar).once(); expect(repository.save(fooBar)).andReturn(fooBarSaved).once(); replay(repository); service.savePreference(key, value); verify(repository); }
@Test public void testSaveKeyValues_new() { final String key = "foo"; List<String> values = new ArrayList<String>(); values.add("bar"); values.add("baz"); PortalPreference fooBar = new PortalPreferenceImpl(key, values); PortalPreferenceImpl fooBarSaved = new PortalPreferenceImpl(key, values); expect(repository.getByKey(key)).andReturn(null).once(); expect(repository.save(fooBar)).andReturn(fooBarSaved).once(); replay(repository); service.savePreference(key, values); verify(repository); }
@Test public void testSaveKeyValues_existing() { final String key = "foo"; List<String> values = new ArrayList<String>(); values.add("bar"); values.add("baz"); List<String> newValues = new ArrayList<String>(); values.add("bar2"); values.add("baz2"); PortalPreferenceImpl fooBar = new PortalPreferenceImpl(key, values); PortalPreferenceImpl fooBarSaved = new PortalPreferenceImpl(key, newValues); expect(repository.getByKey(key)).andReturn(fooBar).once(); expect(repository.save(fooBar)).andReturn(fooBarSaved).once(); replay(repository); service.savePreference(key, values); verify(repository); }
@Test public void testSavePreference() { PortalPreference title = titlePreference(); PortalPreferenceImpl savedTitle = new PortalPreferenceImpl("title", "Rave"); expect(repository.save(title)).andReturn(savedTitle).once(); replay(repository); service.savePreference(title); verify(repository); }