Atec mtg7 unittest

2,324 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,324
On SlideShare
0
From Embeds
0
Number of Embeds
265
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Atec mtg7 unittest

  1. 1. 2011.05.25 ATEC Mtg#72011 5 26
  2. 2. • Twitter id @nowsprinting • • • Java, Swing, Do-Ja, iOS SIer/2011 5 26
  3. 3. Agenda • • JUnit • Android Testing Framework • Android Mock2011 5 26
  4. 4. 2011 5 26
  5. 5. ©snsk at ATEC Mtg#12011 5 26
  6. 6. ©snsk at ATEC Mtg#12011 5 26
  7. 7. 2011 5 26
  8. 8. Activity Http Lib (Mock) Lib (Mock)2011 5 26
  9. 9. → Monkey God Hand Runner Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
  10. 10. → Android Activity Monkey → God Hand Runner →suspend→resume Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
  11. 11. → Monkey Instrumental God Hand Runner TestCase Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
  12. 12. JUnit • Android Testing Framework JUnit3 • TestCase • TestCase Assertion2011 5 26
  13. 13. TestCase package jp.group.android.atec.testter.logic; public class TwitterLogic { } package package jp.group.android.atec.testter.logic; public class TwitterLogicTest extends AndroidTestCase { } Class ”Test” JUnit TestCase2011 5 26
  14. 14. “test” + + public void testTwitterLogicAuthorization() { Authorization auth4test = ... TwitterLogic target = new TwitterLogic(auth4test); assertNotNull("Twitter ", target.twitter); } Assertion2011 5 26
  15. 15. assert()2011 5 26
  16. 16. 2011 5 26
  17. 17. Android Testing Framework2011 5 26
  18. 18. Test Project2011 5 26
  19. 19. eclipse package explorer New Test Project...2011 5 26
  20. 20. Dialog (1/2) TestProject Project2011 5 26
  21. 21. Dialog (2/2) package • • Class package2011 5 26
  22. 22. Why?2011 5 26
  23. 23. Android Testing Framework /2011 5 26
  24. 24. Android Testing Framework /2011 5 26
  25. 25. Android Testing Framework / • • Class package package2011 5 26
  26. 26. TestCase2011 5 26
  27. 27. Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase2011 5 26
  28. 28. Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase Testter • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase2011 5 26
  29. 29. Test Cases (2/2) • InstrumentationTestCase • ActivityTestCase • ActivityInstrumentationTestCase2 • ActivityUnitTestCase • SingleLaunchActivityTestCase • SyncBaseInstrumentation2011 5 26
  30. 30. AndroidTestCase2011 5 26
  31. 31. AndroidTestCase • Activity • Context getContext() Context2011 5 26
  32. 32. public class PreferenceLogicTest extends AndroidTestCase { public void testWriteReadToken() { PreferenceLogic logic = PreferenceLogic.getInstance(); logic.writeToken(getContext(), new AccessToken("11111", "22222")); AccessToken token = logic.readToken(getContext()); assertEquals("11111", token.getToken()); assertEquals("22222", token.getTokenSecret()); } }2011 5 26
  33. 33. ActivityInstrumentation TestCase22011 5 26
  34. 34. ActivityInstrumentation TestCase2 • Activity • Activity • Context getContext() • ActivityInstrumentationTestCase @Deprecated2011 5 26
  35. 35. package jp.group.android.atec.testter;public class AuthActivityTest extends ActivityInstrumentationTestCase2<AuthActivity> { protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(true); mIntent = new Intent(); setActivityIntent(mIntent); mActivity = getActivity(); mEditText = (EditText) mActivity.findViewById(R.id.pinEdit); mButton = (Button) mActivity.findViewById(R.id.registBtn); }2011 5 26
  36. 36. package jp.group.android.atec.testter;public class AuthActivityTest extends ActivityInstrumentationTestCase2<AuthActivity> { protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(true); mIntent = new Intent(); Activity setActivityIntent(mIntent); mActivity = getActivity(); mEditText = (EditText) mActivity.findViewById(R.id.pinEdit); mButton = (Button) mActivity.findViewById(R.id.registBtn); } package res2011 5 26
  37. 37. public void testRegistBtnText() { // assertTrue(mButton.getText().toString() .equalsIgnoreCase(" ")); } Assertion2011 5 26
  38. 38. ※ UI public void testClickButton() { mActivity.runOnUiThread(new Runnable() { public void run() { try { mButton.performClick(); assertXxx(...); } catch (RuntimeException e) { assertXxx(...); } } }); }2011 5 26
  39. 39. private2011 5 26
  40. 40. private private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) }2011 5 26
  41. 41. Class<AuthActivity> clazz = AuthActivity.class; Field field = clazz .getDeclaredField("preferenceLogic"); Method method = getClass() .getMethod("getCacheDrawable"); method.invoke();2011 5 26
  42. 42. CV2011 5 26
  43. 43. Fragile Test private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) } default Package Private2011 5 26
  44. 44. ※ Class<View> clazz = View.class; Field field = clazz.getDeclaredField("mOnClickListener"); field.setAccessible(true); assertTrue(field.get(mButton) != null); AndroidSDK Class field → Mock2011 5 26
  45. 45. 2011 5 26
  46. 46. public AccessToken auth(String pin) { AccessToken accessToken = null; try { (snip) } catch (TwitterException e) { (snip) throw new RuntimeException(e); } return accessToken; } ==2011 5 26
  47. 47. fail try { target.auth(null); fail("Twitter#getOAuthAccessToken() "); } catch (RuntimeException e) { assertEquals("catch ", expected, e.getCause()); }2011 5 26
  48. 48. Android Mock2011 5 26
  49. 49. Android Mock • Dalvk EasyMock • Mock Class2011 5 26
  50. 50. public AccessToken auth(String pin) { AccessToken accessToken = null; try { if (pin == null || "".equals(pin.trim())) { accessToken = twitter.getOAuthAccessToken(); } else { (snip) } } catch (TwitterException e) { (snip) } Twitter4J (snip) }2011 5 26
  51. 51. 1/2 @UsesMocks(Twitter.class) public void testAuth_ _PIN null() { // setup mock AccessToken expectedToken = new AccessToken("testToken", "testSecret"); Twitter Mock Twitter twitterMock = AndroidMock.createMock(Twitter.class); AndroidMock.expect(twitterMock .getOAuthAccessToken()).andReturn(expectedToken); AndroidMock.replay(twitterMock);2011 5 26
  52. 52. 2/2 // setup test target TwitterLogic target = new TwitterLogic(); target.twitter = twitterMock; Mock // do test. AccessToken actual = target.auth(null); assertEquals(" AccessToken ", expectedToken, actual); // Mock AndroidMock.verify(twitterMock); } Mock2011 5 26
  53. 53. @UsesMocks(Twitter.class) public void testAuth_ _ () { // setup mock TwitterException expected = new TwitterException("Unauthorized", null, 401); Twitter twitterMock = AndroidMock .createMock(Twitter.class); AndroidMock.expect(twitterMock .getOAuthAccessToken()).andThrow(expected); AndroidMock.replay(twitterMock);2011 5 26
  54. 54. http://www.slideshare.net/goyoki/an-approach-to-improving-the-maintainability- of-unit-tests-xpjugkansai20112011 5 26
  55. 55. • backlog wiki •2011 5 26
  56. 56. 2011.05.25 20:012011 5 26

×