• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Atec mtg7 unittest
 

Atec mtg7 unittest

on

  • 2,122 views

 

Statistics

Views

Total Views
2,122
Views on SlideShare
1,900
Embed Views
222

Actions

Likes
2
Downloads
14
Comments
0

3 Embeds 222

http://sassylog.blogspot.com 162
http://sassylog.blogspot.jp 58
url_unknown 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Atec mtg7 unittest Atec mtg7 unittest Presentation Transcript

    • 2011.05.25 ATEC Mtg#72011 5 26
    • • Twitter id @nowsprinting • • • Java, Swing, Do-Ja, iOS SIer/2011 5 26
    • Agenda • • JUnit • Android Testing Framework • Android Mock2011 5 26
    • 2011 5 26
    • ©snsk at ATEC Mtg#12011 5 26
    • ©snsk at ATEC Mtg#12011 5 26
    • 2011 5 26
    • Activity Http Lib (Mock) Lib (Mock)2011 5 26
    • → Monkey God Hand Runner Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
    • → Android Activity Monkey → God Hand Runner →suspend→resume Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
    • → Monkey Instrumental God Hand Runner TestCase Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest2011 5 26
    • JUnit • Android Testing Framework JUnit3 • TestCase • TestCase Assertion2011 5 26
    • 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
    • “test” + + public void testTwitterLogicAuthorization() { Authorization auth4test = ... TwitterLogic target = new TwitterLogic(auth4test); assertNotNull("Twitter ", target.twitter); } Assertion2011 5 26
    • assert()2011 5 26
    • 2011 5 26
    • Android Testing Framework2011 5 26
    • Test Project2011 5 26
    • eclipse package explorer New Test Project...2011 5 26
    • Dialog (1/2) TestProject Project2011 5 26
    • Dialog (2/2) package • • Class package2011 5 26
    • Why?2011 5 26
    • Android Testing Framework /2011 5 26
    • Android Testing Framework /2011 5 26
    • Android Testing Framework / • • Class package package2011 5 26
    • TestCase2011 5 26
    • Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase2011 5 26
    • Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase Testter • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase2011 5 26
    • Test Cases (2/2) • InstrumentationTestCase • ActivityTestCase • ActivityInstrumentationTestCase2 • ActivityUnitTestCase • SingleLaunchActivityTestCase • SyncBaseInstrumentation2011 5 26
    • AndroidTestCase2011 5 26
    • AndroidTestCase • Activity • Context getContext() Context2011 5 26
    • 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
    • ActivityInstrumentation TestCase22011 5 26
    • ActivityInstrumentation TestCase2 • Activity • Activity • Context getContext() • ActivityInstrumentationTestCase @Deprecated2011 5 26
    • 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
    • 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
    • public void testRegistBtnText() { // assertTrue(mButton.getText().toString() .equalsIgnoreCase(" ")); } Assertion2011 5 26
    • ※ UI public void testClickButton() { mActivity.runOnUiThread(new Runnable() { public void run() { try { mButton.performClick(); assertXxx(...); } catch (RuntimeException e) { assertXxx(...); } } }); }2011 5 26
    • private2011 5 26
    • private private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) }2011 5 26
    • Class<AuthActivity> clazz = AuthActivity.class; Field field = clazz .getDeclaredField("preferenceLogic"); Method method = getClass() .getMethod("getCacheDrawable"); method.invoke();2011 5 26
    • CV2011 5 26
    • Fragile Test private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) } default Package Private2011 5 26
    • ※ Class<View> clazz = View.class; Field field = clazz.getDeclaredField("mOnClickListener"); field.setAccessible(true); assertTrue(field.get(mButton) != null); AndroidSDK Class field → Mock2011 5 26
    • 2011 5 26
    • public AccessToken auth(String pin) { AccessToken accessToken = null; try { (snip) } catch (TwitterException e) { (snip) throw new RuntimeException(e); } return accessToken; } ==2011 5 26
    • fail try { target.auth(null); fail("Twitter#getOAuthAccessToken() "); } catch (RuntimeException e) { assertEquals("catch ", expected, e.getCause()); }2011 5 26
    • Android Mock2011 5 26
    • Android Mock • Dalvk EasyMock • Mock Class2011 5 26
    • 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
    • 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
    • 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
    • @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
    • http://www.slideshare.net/goyoki/an-approach-to-improving-the-maintainability- of-unit-tests-xpjugkansai20112011 5 26
    • • backlog wiki •2011 5 26
    • 2011.05.25 20:012011 5 26