Upgrade to Pro — share decks privately, control downloads, hide ads and more …

droidkaigi-2016

Yuya Kaido
February 18, 2016

 droidkaigi-2016

Yuya Kaido

February 18, 2016
Tweet

More Decks by Yuya Kaido

Other Decks in Programming

Transcript

  1. ͦΕͧΕͷ໰୊఺ • ๲େͳςετέʔεͷਓྗ֬ೝ • ਓతϦιʔεͷ࿘අ → ػձଛࣦ • ӨڹൣғͷߟྀϛεʹΑΔόά •

    Ϣʔβʔͷ཭୤ → ػձଛࣦ • ςετέʔεͷෳࡶԽ • ਓతϦιʔεͷ࿘අ → ػձଛࣦ 
  2. MVP • ϝϦοτ • ੹຿ͷ෼཭ • Activity/Fragment = View •

    Presenter = Controller • σϝϦοτ • Ϋϥε਺͕ଟ͘ͳΔ • νʔϜ։ൃͷ৔߹͸͋Δఔ౓υΩϡϝϯτ͕ඞཁ 
  3. Contributor @Table
 public class Contributor {
 
 @Column(unique = true,

    uniqueOnConflict = OnConflict.REPLACE)
 @SerializedName("login")
 public String login;
 
 @Column
 @Nullable
 @SerializedName("avatar_url")
 public String avatarUrl;
 
 @Column
 @Nullable
 @SerializedName("html_url")
 public String htmlUrl;
 
 @Column
 @SerializedName("contributions")
 public int contributions;
 
 } 
  4. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  5. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  6. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  7. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  8. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  9. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  10. GithubNetwork public String getBaseUrl() {
 return "https://api.github.com";
 }
 
 public

    Observable<List<Contributor>> getContributors(String owner, String repo) {
 GithubApi githubApi = ApiClientGenerater.generate(GithubApi.class, getBaseUrl());
 return githubApi.getContributors(owner, repo)
 .map(new Func1<List<Contributor>, List<Contributor>>() {
 @Override
 public List<Contributor> call(List<Contributor> contributors) {
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 return contributors;
 }
 });
 }
 
 public Observable<List<Contributor>> getContributors() {
 return getContributors("konifar", "droidkaigi2016");
 } 
  11. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  12. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  13. TestSubscriber Integer[] integers = new Integer[] {1, 2, 3};
 Observable<Integer>

    observable = Observable.from(integers);
 
 observable.subscribe(new Subscriber<Integer>() {
 @Override
 public void onCompleted() {}
 
 @Override
 public void onError(Throwable e) {}
 
 @Override
 public void onNext(Integer integer) {
 throw new RuntimeException();
 }
 }); 
  14. TestSubscriber @Test
 public void subscriberTest() {
 Integer[] integers = new

    Integer[] {1, 2, 3};
 Observable<Integer> observable = Observable.from(integers);
 
 TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
 observable.subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertValueCount(integers.length);
 testSubscriber.assertValues(integers);
 testSubscriber.assertCompleted();
 } 
  15. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  16. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  17. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  18. GithubDatabaseͷςετ final int COUNT = 10;
 List<Contributor> contributors = new

    ArrayList<>(COUNT);
 for (int i = 0; i < COUNT; i++) {
 Contributor contributor = new Contributor();
 contributor.login = String.valueOf(i);
 contributor.avatarUrl = String.valueOf(i);
 contributor.htmlUrl = String.valueOf(i);
 contributor.contributions = i;
 contributors.add(contributor);
 }
 
 Database.getDatabase()
 .prepareInsertIntoContributor()
 .executeAll(contributors);
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 GithubDatabase.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> result = testSubscriber.getOnNextEvents().get(0);
 assertThat(result.size(), is(contributors.size())); 
  19. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  20. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  21. MockWebServer File file = new File( "src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer =

    new MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start(); 
  22. MockWebServer File file = new File( "src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer =

    new MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start(); 
  23. MockWebServer File file = new File( "src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer =

    new MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start(); 
  24. MockWebServer File file = new File( "src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer =

    new MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start(); 
  25. ϝιουͷݺͼग़֬͠ೝ // mock creation
 List mockedList = mock(List.class);
 
 //

    using mock object - it does not throw any "unexpected interaction" exception
 mockedList.add("one");
 mockedList.clear();
 
 // selective, explicit, highly readable verification
 verify(mockedList).add("one");
 verify(mockedList).clear(); 
  26. ϝιουͷݺͼग़֬͠ೝ // mock creation
 List mockedList = mock(List.class);
 
 //

    using mock object - it does not throw any "unexpected interaction" exception
 mockedList.add("one");
 mockedList.clear();
 
 // selective, explicit, highly readable verification
 verify(mockedList).add("one");
 verify(mockedList).clear(); 
  27. ϝιουͷݺͼग़֬͠ೝ // mock creation
 List mockedList = mock(List.class);
 
 //

    using mock object - it does not throw any "unexpected interaction" exception
 mockedList.add("one");
 mockedList.clear();
 
 // selective, explicit, highly readable verification
 verify(mockedList).add("one");
 verify(mockedList).clear(); 
  28. ϝιουͷݺͼग़֬͠ೝ // mock creation
 List mockedList = mock(List.class);
 
 //

    using mock object - it does not throw any "unexpected interaction" exception
 mockedList.add("one");
 mockedList.clear();
 
 // selective, explicit, highly readable verification
 verify(mockedList).add("one");
 verify(mockedList).clear(); 
  29. ϝιουͷฦ໭஋ͷϞοΫ // you can mock concrete classes, not only interfaces


    LinkedList mockedList = mock(LinkedList.class);
 
 // stubbing appears before the actual execution
 when(mockedList.get(0)).thenReturn("first");
 
 // the following is "first"
 assertThat((String) mockedList.get(0), is("first"));
 
 // the following is "null" because get(999) was not stubbed
 assertThat(mockedList.get(999), nullValue()); 
  30. ϝιουͷฦ໭஋ͷϞοΫ // you can mock concrete classes, not only interfaces


    LinkedList mockedList = mock(LinkedList.class);
 
 // stubbing appears before the actual execution
 when(mockedList.get(0)).thenReturn("first");
 
 // the following is "first"
 assertThat((String) mockedList.get(0), is("first"));
 
 // the following is "null" because get(999) was not stubbed
 assertThat(mockedList.get(999), nullValue()); 
  31. ϝιουͷฦ໭஋ͷϞοΫ // you can mock concrete classes, not only interfaces


    LinkedList mockedList = mock(LinkedList.class);
 
 // stubbing appears before the actual execution
 when(mockedList.get(0)).thenReturn("first");
 
 // the following is "first"
 assertThat((String) mockedList.get(0), is("first"));
 
 // the following is "null" because get(999) was not stubbed
 assertThat(mockedList.get(999), nullValue()); 
  32. ϝιουͷฦ໭஋ͷϞοΫ // you can mock concrete classes, not only interfaces


    LinkedList mockedList = mock(LinkedList.class);
 
 // stubbing appears before the actual execution
 when(mockedList.get(0)).thenReturn("first");
 
 // the following is "first"
 assertThat((String) mockedList.get(0), is("first"));
 
 // the following is "null" because get(999) was not stubbed
 assertThat(mockedList.get(999), nullValue()); 
  33. ϝιουͷฦ໭஋ͷϞοΫ // you can mock concrete classes, not only interfaces


    LinkedList mockedList = mock(LinkedList.class);
 
 // stubbing appears before the actual execution
 when(mockedList.get(0)).thenReturn("first");
 
 // the following is "first"
 assertThat((String) mockedList.get(0), is("first"));
 
 // the following is "null" because get(999) was not stubbed
 assertThat(mockedList.get(999), nullValue()); 
  34. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  35. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  36. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  37. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  38. GithubNetworkͷςετ File file = new File("src/test/assets/json/github_contributors.json");
 MockWebServer mockWebServer = new

    MockWebServer();
 mockWebServer.enqueue(ResponseUtil.createMockResponse(file));
 mockWebServer.start();
 
 GithubNetwork githubNetwork = GithubNetwork.getInstance();
 githubNetwork = spy(githubNetwork);
 when(githubNetwork.getBaseUrl())
 .thenReturn(mockWebServer.url("/").toString());
 
 TestSubscriber<List<Contributor>> testSubscriber = new TestSubscriber<>();
 githubNetwork.getContributors().subscribe(testSubscriber);
 
 testSubscriber.assertNoErrors();
 testSubscriber.assertCompleted();
 List<Contributor> contributors = testSubscriber.getOnNextEvents().get(0);
 assertThat(contributors.size(), is(1)); 
  39. ResponseUtil public class ResponseUtil {
 
 public static MockResponse createMockResponse(


    File file) throws IOException {
 BufferedSource source = Okio.buffer(Okio.source(file));
 StringBuilder builder = new StringBuilder();
 while (!source.exhausted()) {
 builder.append(source.readUtf8Line());
 }
 
 MockResponse mockResponse = new MockResponse();
 mockResponse.setBody(builder.toString());
 
 return mockResponse;
 }
 
 }