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

既存のAndroidアプリをリファクタリングしていく話

 既存のAndroidアプリをリファクタリングしていく話

hisaichi5518

July 02, 2016
Tweet

More Decks by hisaichi5518

Other Decks in Technology

Transcript

  1. 1 @RunWith(AndroidJUnit4.class) 2 @LargeTest 3 public class HelloWorldEspressoTest { 4

    5 @Rule 6 public ActivityTestRule<MainActivity> mActivityRule 7 = new ActivityTestRule(MainActivity.class); 8 9 @Test 10 public void listGoesOverTheFold() { 11 onView(withText("Hello world!")).check(matches(isDisplayed())); 12 } 13 }
  2. 1 @RunWith(AndroidJUnit4.class) 2 @LargeTest 3 public class HelloWorldEspressoTest { 4

    5 @Rule 6 public ActivityTestRule<MainActivity> mActivityRule 7 = new ActivityTestRule(MainActivity.class); 8 9 @Test 10 public void listGoesOverTheFold() { 11 onView(withText("Hello world!")).check(matches(isDisplayed())); 12 } 13 }
  3. .7$

  4. .71

  5. .7$

  6. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 }
  7. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 } ೖྗΛड͚औΔ ϓϨθϯςʔγϣϯϩδοΫ 7JFXͷߋ৽
  8. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 } ೖྗΛड͚औΔ
  9. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 } ϓϨθϯςʔγϣϯϩδοΫ
  10. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 } 7JFXͷߋ৽
  11. 1 public class UserActivity extends AppCompatActivity { 2 @Override 3

    protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 fetchUser(userId); 6 } 7 8 private void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 showName(name); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 showError(error); 19 } 20 }); 21 } 22 23 public void showName(String name) { 24 mTextView.setText(name); 25 } 26 27 public void showError(...) { ... } 28 } ೖྗΛड͚औΔ ϓϨθϯςʔγϣϯϩδοΫ 7JFXͷߋ৽
  12. .71

  13. 1 public class UserActivity extends AppCompatActivity implements UserView { 2

    @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 UserPresenter presenter = new UserPresenter(this); 6 presenter.fetchUser(userId); 7 } 8 9 @Override 10 public void showName(String name) { 11 mTextView.setText(name); 12 } 13 14 @Override 15 public void showError(...) { ... } 16 } 6TFS"DUJWJUZ
  14. 1 public class UserActivity extends AppCompatActivity implements UserView { 2

    @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 UserPresenter presenter = new UserPresenter(this); 6 presenter.fetchUser(userId); 7 } 8 9 @Override 10 public void showName(String name) { 11 mTextView.setText(name); 12 } 13 14 @Override 15 public void showError(...) { ... } 16 } 1 public interface UserView { 2 void showName(String name); 3 void showError(...); 4 } 7JFXͷߋ৽ϝιου͕ఆٛ 6TFS7JFX
  15. 1 public class UserPresenter { 2 UserView mView; 3 4

    public UserPresenter(UserView view) { 5 mView = view; 6 } 7 8 public void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 mView.showName(user.getName()); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 mView.showError(error); 19 } 20 }); 21 } 22 } 6TFS1SFTFOUFS
  16. 1 public class UserActivity extends AppCompatActivity implements UserView { 2

    @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 UserPresenter presenter = new UserPresenter(this); 6 presenter.fetchUser(userId); 7 } 8 9 @Override 10 public void showName(String name) { 11 mTextView.setText(name); 12 } 13 14 @Override 15 public void showError(...) { ... } 16 } ೖྗΛड͚औΔ 7JFXͷߋ৽ 6TFS"DUJWJUZ
  17. 1 public class UserActivity extends AppCompatActivity implements UserView { 2

    @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 UserPresenter presenter = new UserPresenter(this); 6 presenter.fetchUser(userId); 7 } 8 9 @Override 10 public void showName(String name) { 11 mTextView.setText(name); 12 } 13 14 @Override 15 public void showError(...) { ... } 16 } ೖྗΛड͚औΔ 6TFS"DUJWJUZ
  18. 1 public class UserPresenter { 2 UserView mView; 3 4

    public UserPresenter(UserView view) { 5 mView = view; 6 } 7 8 public void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 mView.showName(user.getName()); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 mView.showError(error); 19 } 20 }); 21 } 22 } 6TFS1SFTFOUFS ϓϨθϯςʔγϣϯϩδοΫ
  19. 1 public class UserPresenter { 2 UserView mView; 3 4

    public UserPresenter(UserView view) { 5 mView = view; 6 } 7 8 public void fetchUser(int userId) { 9 UserService service = ApiClient.getInstance().create(UsersService.class); 10 service.fetch(userId, new Callback<User>() { 11 @Override 12 public void success(User user, Response response) { 13 mView.showName(user.getName()); 14 } 15 16 @Override 17 public void failure(RetrofitError error) { 18 mView.showError(error); 19 } 20 }); 21 } 22 } 6TFS1SFTFOUFS 6TFS7JFXJOUFSGBDFΛܦ༝ͯ͠7JFXͷߋ৽
  20. 1 public class UserActivity extends AppCompatActivity implements UserView { 2

    @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 int userId = ...; 5 UserPresenter presenter = new UserPresenter(this); 6 presenter.fetchUser(userId); 7 } 8 9 @Override 10 public void showName(String name) { 11 mTextView.setText(name); 12 } 13 14 @Override 15 public void showError(...) { ... } 16 } 7JFXͷߋ৽ 6TFS"DUJWJUZ
  21. 1 public class UserPresenterTest { 2 @Test 3 public void

    testFetchUser() throws Exception { 4 final AtomicBoolean called = new AtomicBoolean(); 5 mMockClient.mock("/v4/users/1.json").to(200, "user.json"); 6 7 class MockedView implements UserView { 8 @Override 9 public void showName(String name) { 10 called.set(true); 11 assertThat(name).isEqualTo("hisaichi5518"); 12 } 13 } 14 15 UserPresenter presenter = new UserPresenter(new MockedView()); 16 presenter.fetchUser(1); 17 assertThat(called.get()).isEqualTo(true); 18 } 19 } 7JFXͷߋ৽͸ JOUFSGBDFΛܦ༝ͯ͠ߦ͏
  22. 1 public class UserPresenterTest { 2 @Test 3 public void

    testFetchUser() throws Exception { 4 final AtomicBoolean called = new AtomicBoolean(); 5 mMockClient.mock("/v4/users/1.json").to(200, "user.json"); 6 7 class MockedView implements UserView { 8 @Override 9 public void showName(String name) { 10 called.set(true); 11 assertThat(name).isEqualTo("hisaichi5518"); 12 } 13 } 14 15 UserPresenter presenter = new UserPresenter(new MockedView()); 16 presenter.fetchUser(1); 17 assertThat(called.get()).isEqualTo(true); 18 } 19 } JOUFSGBDFΛܧঝ͍ͯ͠Δ 7JFX΋Ͳ͖͑͋͞Ε͹ 1SFTFOUFSͷςετ͸Ͱ͖Δ
  23. 1 public class UserPresenterTest { 2 @Test 3 public void

    testFetchUser() throws Exception { 4 final AtomicBoolean called = new AtomicBoolean(); 5 mMockClient.mock("/v4/users/1.json").to(200, "user.json"); 6 7 class MockedView implements UserView { 8 @Override 9 public void showName(String name) { 10 called.set(true); 11 assertThat(name).isEqualTo("hisaichi5518"); 12 } 13 } 14 15 UserPresenter presenter = new UserPresenter(new MockedView()); 16 presenter.fetchUser(1); 17 assertThat(called.get()).isEqualTo(true); 18 } 19 } 7JFX΋Ͳ͖
  24. 1 public class UserPresenterTest { 2 @Test 3 public void

    testFetchUser() throws Exception { 4 final AtomicBoolean called = new AtomicBoolean(); 5 mMockClient.mock("/v4/users/1.json").to(200, "user.json"); 6 7 class MockedView implements UserView { 8 @Override 9 public void showName(String name) { 10 called.set(true); 11 assertThat(name).isEqualTo("hisaichi5518"); 12 } 13 } 14 15 UserPresenter presenter = new UserPresenter(new MockedView()); 16 presenter.fetchUser(1); 17 assertThat(called.get()).isEqualTo(true); 18 } 19 }
  25. 1 public class UserPresenterTest { 2 @Test 3 public void

    testFetchUser() throws Exception { 4 final AtomicBoolean called = new AtomicBoolean(); 5 mMockClient.mock("/v4/users/1.json").to(200, "user.json"); 6 7 class MockedView implements UserView { 8 @Override 9 public void showName(String name) { 10 called.set(true); 11 assertThat(name).isEqualTo("hisaichi5518"); 12 } 13 } 14 15 UserPresenter presenter = new UserPresenter(new MockedView()); 16 presenter.fetchUser(1); 17 assertThat(called.get()).isEqualTo(true); 18 } 19 }