Skip to content

Instantly share code, notes, and snippets.

@martinsson
Created January 19, 2012 10:11
Show Gist options
  • Save martinsson/1639222 to your computer and use it in GitHub Desktop.
Save martinsson/1639222 to your computer and use it in GitHub Desktop.
Solution si jamais on ne peut pas modifier InnerManager
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
public class AccountManager implements IAccountManager {
IAccountInnerManager innerManager;
public AccountManager(IAccountInnerManager innerManager) {
this.innerManager = innerManager;
}
/**
* @deprecated Use {@link InnerManager#purgeAccounts()} instead
*/
public void purgeAccounts() {
innerManager.purgeAccounts();
}
public static class AccountManagerPurgeAccountsTest {
@Test //ce test a perdu beacoup de son sens vu qu'il n'y a plus de logique dans AccountManager
public void testPurgeAccountsWithSeveralPurgeableAccountsInDb() {
IAccountInnerManager innerManager = Mockito.mock(IAccountInnerManager.class);
IAccountManager manager = new AccountManager(innerManager); // injection des dépendances
manager.purgeAccounts();
verify(innerManager).purgeAccounts();
}
@Test
public void purgeAccountsWithSeveralPurgeableAccountsInDb() {
DAO dao = new DAO();
dao.insertRemovable("id1");
dao.insertRemovable("id2");
dao.insertRemNotovable("id3");
InnerManager innerManager = new InnerManager(dao);
innerManager.purgeAccounts();
List<String> accountIds = dao.getAllAccountsIds();
assertThat(accountIds, contains("id3"));
}
}
}
import static java.util.Arrays.asList;
import java.util.List;
class InnerManager implements IAccountInnerManager {
public InnerManager(DAO dao) {
// TODO Auto-generated constructor stub
}
@Transactional
public void purgeExtServiceAccount(String id) {
// TODO Auto-generated method stub
}
@Transactional
public List<String> purgeCustomerAccountsInDb() {
// TODO Auto-generated method stub
return asList();
}
public void purgeAccounts() {
List<String> deletedAccountsId = purgeCustomerAccountsInDb();
for (String id : deletedAccountsId) {
purgeExtServiceAccount(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment