Last active
August 11, 2018 15:24
-
-
Save heridev/b64a8b20f93c0d39a8c8a4eb7c782989 to your computer and use it in GitHub Desktop.
Revisions
-
heridev revised this gist
Aug 11, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -71,7 +71,7 @@ public interface BookActivityView { ``` And as our BooksActivity implements this interface we need to declare there our view method and the logic for that: BooksActivity.java ``` public class BooksActivity extend BaseActivity implements BooksActivityView { -
heridev revised this gist
Aug 11, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ Let's start changing the repository to trigger an exception(typo in the orderby clause): DatabaseBooksRepository.java ``` public class DatabaseBooksRepository implements BooksRepository { -
heridev created this gist
Aug 11, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,123 @@ Let's start changing the repository to trigger an exception(typo in the orderby clause): DatabaseBooksRepository.java ``` public class DatabaseBooksRepository implements BooksRepository { private final DatabaseHelper databaseHelper; public DatabaseBooksRepository(Content context) { databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class); } @Override public List<Book> getBooks(){ try{ return databaseHelper.getbookDaO().queryBuilder() .orderBy("orderXXX", tru) .query(); } catch { throw new RuntimeException("boom"); } } } ``` the new scenario for testing it would look like this: BooksActivityPresenterTest.java ``` import org.junit.Test; import org.junit.Asserts; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; public class BooksActivityPresenterTest { @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock BooksRepository booksRepository; @Mock BooksActivityView view; BooksActivityPresenter presenter; @Before public void setUp() { presenter = new BooksActivityPresenter(view, booksRepository); } @Test public void shouldHandleAnException() { Mockito.when(booksRepository.getBooks()) .thenThrown(new RuntimeException("boom"))); presenter.loadBooks(); Mockito.verify(view).displayError(); } } ``` Now we need to implement this new displayError method: BooksActivityView.js ``` public interface BookActivityView { void displayBooks(List<Book> bookList); void displayNoBooks(); void displayError(); } ``` And as our BooksActivity implements this interface we need to declare there our view method and the logic for that: ``` BooksActivity.java ``` public class BooksActivity extend BaseActivity implements BooksActivityView { private BooksActivityPresenter presenter; @override protected void onCreate(...){ .... } @Override public void displayBooks(List<book> bookList) { ... } @Override public void displayNoBooks() { ... }; @Override public void displayError() { Toast.makeText(this, "Errror accessing data", Toast.LENGTH_SHORT).show(); }; } ``` And we need to reflect this change on the main: BooksActivityPresenter.java ``` public class BooksActivityPresenter { public BooksActivityPresenter(BooksActivityView view, BooksRepository booksRepository) { this.view = view; this.booksRepository = booksRepository } public void loadBooks(){ List<Book> listBooks; try { listBooks = booksRepository.getBooks(); // Here the presenter does not know anything about the repository // the presenter just wait for getting an array of values and that's it if(listBooks.isEmpty()){ view.displayNoBooks(); } else { view.displayBooks(bookList); } } catch(Exception e) { view.displayError(); } } } ```