Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Android Testing
Evan Lin
at
evan.lin@getloopd.com
Tools
• Mock -> Mockito
• http://mockito.org/
• UI Testing -> Espresso
• https://developer.android.com/training/testing/ui...
Mockito
dependencies {

...


// Mockito

debugCompile 'org.mockito:mockito-core:1.10.19'

debugCompile 'com.google.dexmak...
Espresso
android {



defaultConfig {


...


// Espresso2

testInstrumentationRunner "android.support.test.runner.Android...
Dagger2
// dagger2
apply plugin: 'com.neenbedankt.android-apt'



buildscript {

repositories {

jcenter()

}

dependencie...
Start
public class MainActivity extends Activity {





@Override

protected void onCreate(Bundle savedInstanceState) {

super.o...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;



@Override

protec...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;



@Override

protec...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;



@Override

protec...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;



@Override

protec...
Keep Signin
public class UserService {



...



public boolean isLogin() {

return loadLoginStatus();

}



public boolean login(Stri...
public class UserService {



...



public boolean isLogin() {

return loadLoginStatus();

}



public boolean login(Stri...
public class UserService {



...



public boolean isLogin() {

return loadLoginStatus();

}



public boolean login(Stri...
public class UserService {



private PasswordValidator mPasswordValidator;

...

public boolean isLogin() {

return loadL...
public class PasswordValidator {



public boolean checkEmailAndPassword(String email, String password) {

return email.eq...
public class UserService {



public static final String SHARED_PREF_NAME = "MY_SHARED_PREF";

private static final String...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;



@Override

protec...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;

UserService mUserSe...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;

UserService mUserSe...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;

UserService mUserSe...
public class MainActivity extends Activity {



EditText mEmailEditText;

EditText mPasswordEditText;

UserService mUserSe...
First Time
Open App
Second Time
Open App
Time to Test
Unit Test
1.
2.
3.
public class UserServiceTest extends TestCase {



}
public class UserServiceTest extends TestCase {



public void testLoginSuccess() throws Exception {

// arrange



// act...
public class UserServiceTest extends TestCase {



public void testLoginSuccess() throws Exception {

// arrange



// act...
public class UserServiceTest extends TestCase {



public void testLoginSuccess() throws Exception {

// arrange



// act...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
public class UserServiceTest extends AndroidTestCase {



public void testLoginSuccess() throws Exception {

// arrange

P...
• passed when running separately(sometimes?)
• passed when running separately(sometimes?)
• but failed running the whole tests
• passed when running separately(sometimes?)
• but failed running the whole tests
• WHY?
public boolean isLogin() {

return loadLoginStatus();

}



public boolean login(String email, String password) {

if (mPa...
public class UserServiceTest extends AndroidTestCase {



public void setUp() throws Exception {

super.setUp();

clearSha...
Works!!
Async Tasks
public class PasswordValidator {



public boolean checkEmailAndPassword(String email, String password) {

return email.eq...
public class PasswordValidator {



public boolean checkEmailAndPassword(String email, String password) {

return email.eq...
public class UserApiService {



public void login(String email, String password, Callback callback) {

// todo: send logi...
public class UserApiService {



public void login(String email, String password, Callback callback) {

if (checkEmailAndP...
public class UserService {



private PasswordValidator mPasswordValidator;

...
public UserService(Context appContext, Pa...
public class UserService {



private UserApiService mUserApiService;

...
public UserService(Context appContext, UserApiS...
public class MainActivity extends Activity {


...



@Override

protected void onCreate(Bundle savedInstanceState) {

sup...
public class MainActivity extends Activity {


...



@Override

protected void onCreate(Bundle savedInstanceState) {

sup...
Unit Test
public void testLoginSuccess() throws Exception {

// arrange

PasswordValidator validator = mock(PasswordValidator.class)...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
public void testLoginSuccess() throws Exception {

// arrange

UserApiService userApiService = mock(UserApiService.class);...
Works!!
UI Test
1.
2.
3.
public class MainActivityTest extends TestCase {



}
public class MainActivityTest extends TestCase {



}
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {



public MainActivityTest() {

sup...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {



public MainActivityTest() {

sup...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLayout() thr...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

...


public void testLoginSuccess...
after doing some research…
use Dagger2 for Dependency Injection
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...
public class MainActivity extends AppCompatActivity {

...



@Inject

UserService mUserService;



@Override

protected v...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
p...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
p...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
K...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
K...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
K...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
K...


@Provides

@Singleton

UserService provideTestUserService(UserApiService userApiService){

return mMockUserService;

}
K...
public class MainActivityTest extends MainActivityTestCase {

...


public void testLoginSuccess() throws Exception {

// ...
public class MainActivityTest extends MainActivityTestCase {

...


public void testLoginSuccess() throws Exception {

// ...
public class MainActivityTest extends MainActivityTestCase {

...


public void testLoginSuccess() throws Exception {

// ...
public class MainActivityTest extends MainActivityTestCase {

...


public void testLoginSuccess() throws Exception {

// ...
public class MainActivityTest extends MainActivityTestCase {

...


public void testLoginSuccess() throws Exception {

// ...
• sometimes failed
• static variable
• local storage
• not easy to test all stuff
• no a best way you can follow
frequent ...
• Pattern (MVP, DI)
• Structure
• follow up the android geeks
what I learned
• mock object called when activity onCreate
• UI testing for a fragment with interaction with
certain activity
• find out t...
• https://github.com/proevan/android-testing-sample
Android Testing
Android Testing
Android Testing
Prochain SlideShare
Chargement dans…5
×

Android Testing

android unit and UI testing sample using Mockito, Espresso and Dagger2

  • Identifiez-vous pour voir les commentaires

Android Testing

  1. 1. Android Testing Evan Lin at evan.lin@getloopd.com
  2. 2. Tools • Mock -> Mockito • http://mockito.org/ • UI Testing -> Espresso • https://developer.android.com/training/testing/ui-testing/espresso-testing.html • Dependency Injection -> Dagger2 • http://google.github.io/dagger/
  3. 3. Mockito dependencies {
 ... 
 // Mockito
 debugCompile 'org.mockito:mockito-core:1.10.19'
 debugCompile 'com.google.dexmaker:dexmaker:1.2'
 debugCompile ‘com.google.dexmaker:dexmaker-mockito:1.2' 
 }
  4. 4. Espresso android {
 
 defaultConfig { 
 ... 
 // Espresso2
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }
 } 
 dependencies { 
 ... 
 // Espresso2
 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
 androidTestCompile ‘com.android.support.test:runner:0.3' 
 }
  5. 5. Dagger2 // dagger2 apply plugin: 'com.neenbedankt.android-apt'
 
 buildscript {
 repositories {
 jcenter()
 }
 dependencies {
 // dagger2
 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
 }
 }
 
 dependencies { 
 ... 
 // dagger2
 compile 'junit:junit:4.12'
 compile 'com.google.dagger:dagger:2.0'
 apt 'com.google.dagger:dagger-compiler:2.0'
 provided 'org.glassfish:javax.annotation:10.0-b28'
 }

  6. 6. Start
  7. 7. public class MainActivity extends Activity {
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 }
 EditText mEmailEditText;
 EditText mPasswordEditText;
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 
 public void onEnterButtonClick() {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
  8. 8. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 }
 
 public void onEnterButtonClick() {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  9. 9. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  10. 10. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  11. 11. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  12. 12. Keep Signin
  13. 13. public class UserService {
 
 ...
 
 public boolean isLogin() {
 return loadLoginStatus();
 }
 
 public boolean login(String email, String password) {
 if (checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 ...
 }
  14. 14. public class UserService {
 
 ...
 
 public boolean isLogin() {
 return loadLoginStatus();
 }
 
 public boolean login(String email, String password) {
 if (checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 ...
 }
  15. 15. public class UserService {
 
 ...
 
 public boolean isLogin() {
 return loadLoginStatus();
 }
 
 public boolean login(String email, String password) {
 if (checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 ...
 }
  16. 16. public class UserService {
 
 private PasswordValidator mPasswordValidator;
 ...
 public boolean isLogin() {
 return loadLoginStatus();
 }
 
 public boolean login(String email, String password) {
 if (mPasswordValidator.checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 ... }
  17. 17. public class PasswordValidator {
 
 public boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 }
  18. 18. public class UserService {
 
 public static final String SHARED_PREF_NAME = "MY_SHARED_PREF";
 private static final String SHARED_PREF_KEY_IS_LOGIN = “SHARED_PREF_KEY_IS_LOGIN";
 private PasswordValidator mPasswordValidator;
 private SharedPreferences mSharedPreferences;
 
 public UserService(Context appContext, PasswordValidator validator) { mPasswordValidator = validator;
 mSharedPreferences = appContext.getSharedPreferences(SHARED_PREF_NAME, 0);
 }
 
 ...
 
 private boolean loadLoginStatus() {
 return mSharedPreferences.getBoolean(SHARED_PREF_KEY_IS_LOGIN, false);
 }
 
 private void saveLoginStatus(boolean isLogin) {
 SharedPreferences.Editor editor = mSharedPreferences.edit();
 editor.putBoolean(SHARED_PREF_KEY_IS_LOGIN, isLogin);
 editor.commit();
 }
 }
  19. 19. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password);
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  20. 20. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 UserService mUserService; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new PasswordValidator());
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  21. 21. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new PasswordValidator());
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 if (checkEmailAndPassword(email, password))
 goToSecondPage();
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  22. 22. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new PasswordValidator());
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 boolean isLoginSuccess = mUserService.login(email, password);
 if (isLoginSuccess)
 goToSecondPage();
 }
 
 private void goToSecondPage() {
 Intent intent = new Intent(this, SecondActivity.class);
 startActivity(intent);
 }
 }
  23. 23. public class MainActivity extends Activity {
 
 EditText mEmailEditText;
 EditText mPasswordEditText;
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new PasswordValidator());
 if (checkAutoLogin()) {
 goToSecondPage();
 finish();
 }
 }
 
 private boolean checkAutoLogin() {
 return mUserService.isLogin();
 } 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 boolean isLoginSuccess = mUserService.login(email, password);
 if (isLoginSuccess)
 goToSecondPage();
 } ...
  24. 24. First Time Open App Second Time Open App
  25. 25. Time to Test
  26. 26. Unit Test
  27. 27. 1. 2. 3.
  28. 28. public class UserServiceTest extends TestCase {
 
 }
  29. 29. public class UserServiceTest extends TestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 
 // assert
 
 }
 
 }
  30. 30. public class UserServiceTest extends TestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 
 // assert
 
 }
 
 } test method prefix
  31. 31. public class UserServiceTest extends TestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 
 // assert
 
 }
 
 }
  32. 32. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = new PasswordValidator();
 UserService userService = new UserService(getContext(), validator);
 
 // act
 
 // assert
 
 }
 
 }
  33. 33. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = new PasswordValidator();
 UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("admin", "admin");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 }
  34. 34. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = Mockito.mock(PasswordValidator.class); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("admin", "admin");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 }
  35. 35. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = Mockito.mock(PasswordValidator.class); Mockito.when(validator.checkEmailAndPassword(Matchers.anyString(), 
 Matchers.anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("admin", "admin");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 }
  36. 36. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = Mockito.mock(PasswordValidator.class); Mockito.when(validator.checkEmailAndPassword(Matchers.anyString(), 
 Matchers.anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 }
  37. 37. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = Mockito.mock(PasswordValidator.class); Mockito.when(validator.checkEmailAndPassword(Matchers.anyString(), 
 Matchers.anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 }
  38. 38. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = mock(PasswordValidator.class); when(validator.checkEmailAndPassword(anyString(), 
 anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 public void testLoginFailure() throws Exception {
 // arrange
 PasswordValidator validator = mock(PasswordValidator.class); when(validator.checkEmailAndPassword(anyString(), 
 anyString())).thenReturn(false); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertFalse(userService.isLogin()); }
 
 }
  39. 39. public class UserServiceTest extends AndroidTestCase {
 
 public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = mock(PasswordValidator.class); when(validator.checkEmailAndPassword(anyString(), 
 anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertTrue(userService.isLogin()); }
 
 public void testLoginFailure() throws Exception {
 // arrange
 PasswordValidator validator = mock(PasswordValidator.class); when(validator.checkEmailAndPassword(anyString(), 
 anyString())).thenReturn(false); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertFalse(userService.isLogin()); }
 
 }
  40. 40. • passed when running separately(sometimes?)
  41. 41. • passed when running separately(sometimes?) • but failed running the whole tests
  42. 42. • passed when running separately(sometimes?) • but failed running the whole tests • WHY?
  43. 43. public boolean isLogin() {
 return loadLoginStatus();
 }
 
 public boolean login(String email, String password) {
 if (mPasswordValidator.checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 }
 
 private boolean loadLoginStatus() {
 return mSharedPreferences.getBoolean(SHARED_PREF_KEY_IS_LOGIN, false);
 }
 
 private void saveLoginStatus(boolean isLogin) {
 SharedPreferences.Editor editor = mSharedPreferences.edit();
 editor.putBoolean(SHARED_PREF_KEY_IS_LOGIN, isLogin);
 editor.commit();
 }
  44. 44. public class UserServiceTest extends AndroidTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 clearSharedPref();
 }
 
 private void clearSharedPref() {
 getContext().getSharedPreferences(UserService.SHARED_PREF_NAME, 0)
 .edit().clear().commit();
 }
 ...
  45. 45. Works!!
  46. 46. Async Tasks
  47. 47. public class PasswordValidator {
 
 public boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 }
  48. 48. public class PasswordValidator {
 
 public boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 } public class UserApiService {
 
 public void login(String email, String password, Callback callback) {
 // todo: send login request to server...
 }
 
 public interface Callback {
 void onSuccess();
 void onFailure();
 }
 }
  49. 49. public class UserApiService {
 
 public void login(String email, String password, Callback callback) {
 // todo: send login request to server...
 }
 
 public interface Callback {
 void onSuccess();
 void onFailure();
 }
 }
  50. 50. public class UserApiService {
 
 public void login(String email, String password, Callback callback) {
 if (checkEmailAndPassword(email, password))
 wait2SecondsAndRespondSuccess(callback);
 }
 
 private boolean checkEmailAndPassword(String email, String password) {
 return email.equals("admin") && password.equals("admin");
 }
 
 private void wait2SecondsAndRespondSuccess(final Callback callback) {
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 callback.onSuccess();
 }
 }, 2000);
 }
 
 public interface Callback {
 void onSuccess();
 void onFailure();
 }
 }

  51. 51. public class UserService {
 
 private PasswordValidator mPasswordValidator;
 ... public UserService(Context appContext, PasswordValidator validator) { mPasswordValidator = validator;
 ...
 } 
 public boolean login(String email, String password) {
 if (mPasswordValidator.checkEmailAndPassword(email, password)) {
 saveLoginStatus(true);
 return true;
 } else {
 return false;
 }
 } 
 ... }
  52. 52. public class UserService {
 
 private UserApiService mUserApiService;
 ... public UserService(Context appContext, UserApiService userApiService) { mUserApiService = userApiService;
 ...
 } 
 public void login(String email, String password, final Callback callback) {
 mUserApiService.login(email, password, new Callback() {
 @Override
 public void onSuccess() {
 saveLoginStatus(true);
 callback.onSuccess();
 }
 
 @Override
 public void onFailure() {
 callback.onFailure();
 }
 });
 }
 ...
  53. 53. public class MainActivity extends Activity { 
 ...
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new PasswordValidator());
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 boolean isLoginSuccess = mUserService.login(email, password);
 if (isLoginSuccess)
 goToSecondPage();
 }
 
 ...
  54. 54. public class MainActivity extends Activity { 
 ...
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEmailEditText = (EditText) findViewById(R.id.email);
 mPasswordEditText = (EditText) findViewById(R.id.password); mUserService = new UserService(getApplicationContext(), new UserApiService());
 }
 
 public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 }
 
 ... 

  55. 55. Unit Test
  56. 56. public void testLoginSuccess() throws Exception {
 // arrange
 PasswordValidator validator = mock(PasswordValidator.class); when(validator.checkEmailAndPassword(anyString(), 
 anyString())).thenReturn(true); UserService userService = new UserService(getContext(), validator);
 
 // act
 userService.login("whateverEmail", "whateverPassword");
 
 // assert
 assertTrue(userService.isLogin()); }

  57. 57. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  58. 58. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  59. 59. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  60. 60. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  61. 61. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  62. 62. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
  63. 63. public void testLoginSuccess() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertTrue(userService.isLogin());
 }
 
 public void testLoginFailure() throws Exception {
 // arrange
 UserApiService userApiService = mock(UserApiService.class);
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 Callback callback = (Callback) invocation.getArguments()[2];
 callback.onFailure();
 return null;
 }
 }).when(userApiService).login(anyString(), anyString(), any(Callback.class));
 UserService userService = new UserService(getContext(), userApiService);
 
 // act
 userService.login("whateverEmail", "whateverPassword", mock(Callback.class));
 
 // assert
 assertFalse(userService.isLogin());
 }
  64. 64. Works!!
  65. 65. UI Test
  66. 66. 1. 2. 3. public class MainActivityTest extends TestCase {
 
 }
  67. 67. public class MainActivityTest extends TestCase {
 
 }
  68. 68. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 
 public MainActivityTest() {
 super(MainActivity.class);
 } 
 }
  69. 69. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 
 public MainActivityTest() {
 super(MainActivity.class);
 } 
 public void setUp() throws Exception {
 super.setUp();
 clearSharedPref();
 getActivity();
 }
 
 private void clearSharedPref() {
 getInstrumentation().getTargetContext().getSharedPreferences (UserService.SHARED_PREF_NAME, 0).edit().clear().commit();
 }
 }
  70. 70. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 Espresso.onView(ViewMatchers.withId(R.id.email))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 Espresso.onView(ViewMatchers.withId(R.id.password))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 Espresso.onView(ViewMatchers.withId(R.id.enter_button))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 } 
 }
  71. 71. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 Espresso.onView(ViewMatchers.withId(R.id.email))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 Espresso.onView(ViewMatchers.withId(R.id.password))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 Espresso.onView(ViewMatchers.withId(R.id.enter_button))
 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
 } 
 }
  72. 72. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 onView(withId(R.id.email))
 .check(matches(isDisplayed()));
 onView(withId(R.id.password))
 .check(matches(isDisplayed()));
 onView(withId(R.id.enter_button))
 .check(matches(isDisplayed()));
 } 
 }
  73. 73. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 onView(withId(R.id.email))
 .check(matches(isDisplayed()));
 onView(withId(R.id.password))
 .check(matches(isDisplayed()));
 onView(withId(R.id.enter_button))
 .check(matches(isDisplayed()));
 } 
 }
  74. 74. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 onView(withId(R.id.email))
 .check(matches(isDisplayed()));
 onView(withId(R.id.password))
 .check(matches(isDisplayed()));
 onView(withId(R.id.enter_button))
 .check(matches(isDisplayed()));
 } 
 }
  75. 75. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 onView(withId(R.id.email))
 .check(matches(isDisplayed()));
 onView(withId(R.id.password))
 .check(matches(isDisplayed()));
 onView(withId(R.id.enter_button))
 .check(matches(isDisplayed()));
 } 
 }
  76. 76. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLayout() throws Exception {
 // arrange
 
 // act
 
 // assert
 onView(withId(R.id.email))
 .check(matches(isDisplayed()));
 onView(withId(R.id.password))
 .check(matches(isDisplayed()));
 onView(withId(R.id.enter_button))
 .check(matches(isDisplayed()));
 } 
 } Works!!
  77. 77. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(ViewMatchers.withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(ViewMatchers.withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(ViewMatchers.withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  78. 78. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(ViewMatchers.withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(ViewMatchers.withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  79. 79. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(ViewMatchers.withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  80. 80. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  81. 81. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  82. 82. public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  83. 83. after doing some research…
  84. 84. use Dagger2 for Dependency Injection
  85. 85. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 }
  86. 86. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 }
  87. 87. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 } @Provides
 @Singleton
 UserService provideUserService(UserApiService userApiService){
 return new UserService(mAppContext, userApiService);
 } release
  88. 88. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 } @Provides
 @Singleton
 UserService provideUserService(UserApiService userApiService){
 return new UserService(mAppContext, userApiService);
 } release
  89. 89. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 } 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } @Provides
 @Singleton
 UserService provideUserService(UserApiService userApiService){
 return new UserService(mAppContext, userApiService);
 } debug
  90. 90. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 } 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } @Provides
 @Singleton
 UserService provideUserService(UserApiService userApiService){
 return new UserService(mAppContext, userApiService);
 } debug
  91. 91. public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {
 String email = mEmailEditText.getText().toString();
 String password = mPasswordEditText.getText().toString();
 mUserService.login(email, password, new UserApiService.Callback() {
 @Override
 public void onSuccess() {
 goToSecondPage();
 }
 
 @Override
 public void onFailure() {
 
 }
 });
 } 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } @Provides
 @Singleton
 UserService provideUserService(UserApiService userApiService){
 return new UserService(mAppContext, userApiService);
 } debug Keep Instance
  92. 92. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 }
  93. 93. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 }
  94. 94. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 } public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {

  95. 95. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } Keep Instance public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 } public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {

  96. 96. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } Keep Instance public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 } public class MainActivity extends AppCompatActivity {
 ...
 
 @Inject
 UserService mUserService;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 UserServiceComponent.Initializer.init(getApplicationContext()).inject(this);
 ... } public void onEnterButtonClick(View view) {

  97. 97. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } Keep Instance public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 }
  98. 98. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } Keep Instance public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 } public class MainActivityTestCase extends ActivityInstrumentationTestCase2<MainActivity> {
 
 @Inject
 protected UserService mMockUserService;
 
 public MainActivityTestCase() {
 super(MainActivity.class);
 }
 }

  99. 99. 
 @Provides
 @Singleton
 UserService provideTestUserService(UserApiService userApiService){
 return mMockUserService;
 } Keep Instance public class MainActivityTest extends MainActivityTestCase {
 
 public void setUp() throws Exception {
 super.setUp();
 getActivity();
 UserServiceComponent.Initializer.getInstance().inject(this);
 } public class MainActivityTestCase extends ActivityInstrumentationTestCase2<MainActivity> {
 
 @Inject
 protected UserService mMockUserService;
 
 public MainActivityTestCase() {
 super(MainActivity.class);
 }
 }

  100. 100. public class MainActivityTest extends MainActivityTestCase {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 
 // act
 onView(ViewMatchers.withId(R.id.email))
 .perform(typeText("whateverEmail"));
 onView(ViewMatchers.withId(R.id.password))
 .perform(typeText("whateverPassword"));
 onView(ViewMatchers.withId(R.id.enter_button))
 .perform(click());
 
 // assert
 onView(withText("Second Page"))
 .check(matches(isDisplayed()));
 } 
 }
  101. 101. public class MainActivityTest extends MainActivityTestCase {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 UserApiService.Callback callback = (UserApiService.Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(mMockUserService).login(anyString(), anyString(), any(UserApiService.Callback.class));
 
 // act
 onView(withId(R.id.email))
 .perform(ViewActions.typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(ViewActions.typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(ViewActions.click());
 
 // assert
 onView(withText("Second Page"))
 .check(ViewAssertions.matches(isDisplayed()));
 }
 ...
  102. 102. public class MainActivityTest extends MainActivityTestCase {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 UserApiService.Callback callback = (UserApiService.Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(mMockUserService).login(anyString(), anyString(), any(UserApiService.Callback.class));
 
 // act
 onView(withId(R.id.email))
 .perform(ViewActions.typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(ViewActions.typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(ViewActions.click());
 
 // assert
 onView(withText("Second Page"))
 .check(ViewAssertions.matches(isDisplayed()));
 }
 ...
  103. 103. public class MainActivityTest extends MainActivityTestCase {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 UserApiService.Callback callback = (UserApiService.Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(mMockUserService).login(anyString(), anyString(), any(UserApiService.Callback.class));
 
 // act
 onView(withId(R.id.email))
 .perform(ViewActions.typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(ViewActions.typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(ViewActions.click());
 
 // assert
 onView(withText("Second Page"))
 .check(ViewAssertions.matches(isDisplayed()));
 }
 ...
  104. 104. public class MainActivityTest extends MainActivityTestCase {
 ... 
 public void testLoginSuccess() throws Exception {
 // arrange
 doAnswer(new Answer() {
 @Override
 public Object answer(InvocationOnMock invocation) throws Throwable {
 UserApiService.Callback callback = (UserApiService.Callback) invocation.getArguments()[2];
 callback.onSuccess();
 return null;
 }
 }).when(mMockUserService).login(anyString(), anyString(), any(UserApiService.Callback.class));
 
 // act
 onView(withId(R.id.email))
 .perform(ViewActions.typeText("whateverEmail"));
 onView(withId(R.id.password))
 .perform(ViewActions.typeText("whateverPassword"));
 onView(withId(R.id.enter_button))
 .perform(ViewActions.click());
 
 // assert
 onView(withText("Second Page"))
 .check(ViewAssertions.matches(isDisplayed()));
 }
 ... Works!!
  105. 105. • sometimes failed • static variable • local storage • not easy to test all stuff • no a best way you can follow frequent problems
  106. 106. • Pattern (MVP, DI) • Structure • follow up the android geeks what I learned
  107. 107. • mock object called when activity onCreate • UI testing for a fragment with interaction with certain activity • find out the best test sturcture still need to figure out
  108. 108. • https://github.com/proevan/android-testing-sample

×