### Question:
MongoDbAuthorityRepository implements AuthorityRepository { @Override public Authority save(Authority item) { Authority fromDb = getByAuthority(item.getAuthority()); Authority save; if(fromDb == null) { save = converter.convert(item, Authority.class); } else { fromDb.setDefaultForNewUser(item.isDefaultForNewUser()); save=fromDb; } template.save(save, AUTHORITY_COLLECTION); return save; }  @Override Authority getByAuthority(String authorityName); @Override List<Authority> getAll(); @Override List<Authority> getLimitedList(int offset, int limit); @Override List<Authority> getAllDefault(); @Override int getCountAll(); @Override Class<? extends Authority> getType(); @Override Authority get(String id); @Override Authority save(Authority item); @Override void delete(Authority item); void setTemplate(MongoOperations template); void setConverter(HydratingConverterFactory converter); MongoOperations getTemplate(); HydratingConverterFactory getConverter(); static final Class<MongoDbAuthority> CLASS; }### Answer:
@Test public void save_validNew(){ Authority returnedAuth; Authority savedAuth = new MongoDbAuthority(); savedAuth.setAuthority("test"); template.save(isA(Authority.class), eq(AUTHORITY_COLLECTION)); expectLastCall(); expect(template.findOne(query(where("authority").is("test")), CLASS, AUTHORITY_COLLECTION)).andReturn(null); replay(template); expect(converter.convert(savedAuth, Authority.class)).andReturn(savedAuth); replay(converter); returnedAuth = repo.save(savedAuth); assertNotNull(template); assertThat(returnedAuth, is(equalTo(savedAuth))); verify(template); }
@Test public void save_valid(){ Authority savedAuth = new MongoDbAuthority(); Authority returnedAuth; savedAuth.setAuthority("test"); expect(template.findOne(query(where("authority").is("test")), CLASS, AUTHORITY_COLLECTION)).andReturn((MongoDbAuthority)savedAuth); template.save(isA(Authority.class), eq(AUTHORITY_COLLECTION)); expectLastCall(); replay(template); returnedAuth = repo.save(savedAuth); assertThat(savedAuth.isDefaultForNewUser(), is(equalTo(returnedAuth.isDefaultForNewUser()))); assertNotNull(template); assertThat(savedAuth, is(sameInstance(returnedAuth))); verify(template); }