SlideShare une entreprise Scribd logo
1  sur  56
Télécharger pour lire hors ligne
2011.05.25 ATEC Mtg#7




2011   5   26
• Twitter id @nowsprinting
                •
                •
                •        Java, Swing, Do-Ja, iOS   SIer/



2011   5   26
Agenda

                •
                • JUnit
                • Android Testing Framework
                • Android Mock

2011   5   26
2011   5   26
©snsk   at ATEC Mtg#1
2011   5   26
©snsk   at ATEC Mtg#1
2011   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 AndroidTest
2011   5   26
→
                                   Android
                                              Activity
                                           Monkey
                                             →    God Hand
                                           Runner
                                         →suspend→resume
                              Activity
                               Http



                   Lib       (Mock)
                   Lib       (Mock)

                Robolectric AndroidTest
2011   5   26
→



                                               Monkey
                Instrumental                          God Hand
                                               Runner
                 TestCase
                                    Activity
                                     Http



                      Lib          (Mock)
                      Lib          (Mock)

                  Robolectric AndroidTest
2011   5   26
JUnit

                • Android Testing Framework   JUnit3

                • TestCase
                • TestCase        Assertion




2011   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   TestCase
2011   5   26
“test” +          +

 public void testTwitterLogicAuthorization() {
   Authorization auth4test = ...

       TwitterLogic target = new TwitterLogic(auth4test);

       assertNotNull("Twitter                          ",
                target.twitter);
 }
           Assertion




2011   5   26
assert()

2011   5   26
2011   5   26
Android Testing
                 Framework



2011   5   26
Test Project



2011   5   26
eclipse           package explorer




                          New Test Project...


2011   5   26
Dialog (1/2)
       TestProject




                                    Project




2011   5   26
Dialog (2/2)




                                      package

                •

                •   Class package

2011   5   26
Why?



2011   5   26
Android Testing Framework   /




2011   5   26
Android Testing Framework   /




2011   5   26
Android Testing Framework    /




           •

           •    Class package
                          package




2011   5   26
TestCase



2011   5   26
Test Cases (1/2)
                • AndroidTestCase
                   • ApplicationTestCase
                   • LoaderTestCase
                   • ProviderTestCase2
                   • ServiceTestCase
           ※        JUnit TestCase



2011   5   26
Test Cases (1/2)
                • AndroidTestCase
                   • ApplicationTestCase
                   • LoaderTestCase        Testter
                   • ProviderTestCase2
                   • ServiceTestCase
           ※        JUnit TestCase



2011   5   26
Test Cases (2/2)
                • InstrumentationTestCase
                    • ActivityTestCase
                        • ActivityInstrumentationTestCase2
                        • ActivityUnitTestCase
                    • SingleLaunchActivityTestCase
                    • SyncBaseInstrumentation

2011   5   26
AndroidTestCase



2011   5   26
AndroidTestCase

                •                             Activity

                • Context      getContext()
                            Context




2011   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
                       TestCase2


2011   5   26
ActivityInstrumentation
                       TestCase2
                • Activity
                • Activity

                • Context         getContext()

                • ActivityInstrumentationTestCase
                  @Deprecated

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();
         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 res
2011   5   26
public void testRegistBtnText() {
  //
           assertTrue(mButton.getText().toString()
                                 .equalsIgnoreCase("   "));
  }
                        Assertion




2011   5   26
※       UI
            public void testClickButton() {

                mActivity.runOnUiThread(new Runnable() {
                  public void run() {
                    try {
                      mButton.performClick();
                      assertXxx(...);
                    } catch (RuntimeException e) {
                      assertXxx(...);
                    }
                  }
                });

            }
2011   5   26
private



2011   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
CV




2011   5   26
Fragile Test



       private final PreferenceLogic preferenceLogic
                       = PreferenceLogic.getInstance();



       private Drawable getCacheDrawable(URL url) {
         (snip)
       }




                  default Package Private

2011   5   26
※

           Class<View> clazz = View.class;
           Field field =
             clazz.getDeclaredField("mOnClickListener");
           field.setAccessible(true);
           assertTrue(field.get(mButton) != null);



                AndroidSDK      Class             field
                    →            Mock



2011   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 Mock



2011   5   26
Android Mock

                • Dalvk      EasyMock



                • Mock     Class




2011   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);
       }

                Mock

2011   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-xpjugkansai2011
2011   5   26
• backlog   wiki

                •


2011   5   26
2011.05.25 20:01


2011   5   26

Contenu connexe

Tendances

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
Deep dive into Android async operations
Deep dive into Android async operationsDeep dive into Android async operations
Deep dive into Android async operationsMateusz Grzechociński
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentationSanjib Dhar
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 KotlinVMware Tanzu
 
Sword fighting with Dagger GDG-NYC Jan 2016
 Sword fighting with Dagger GDG-NYC Jan 2016 Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016Mike Nakhimovich
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019Sam Brannen
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseCoen De Roover
 
Dagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaDagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaEdson Menegatti
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
Android MvRx Framework 介紹
Android MvRx Framework 介紹Android MvRx Framework 介紹
Android MvRx Framework 介紹Kros Huang
 
Android Loaders : Reloaded
Android Loaders : ReloadedAndroid Loaders : Reloaded
Android Loaders : Reloadedcbeyls
 
Introduction to TDD with FlexUnit
Introduction to TDD with FlexUnitIntroduction to TDD with FlexUnit
Introduction to TDD with FlexUnitAnupom Syam
 

Tendances (16)

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
Deep dive into Android async operations
Deep dive into Android async operationsDeep dive into Android async operations
Deep dive into Android async operations
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 Kotlin
 
Sword fighting with Dagger GDG-NYC Jan 2016
 Sword fighting with Dagger GDG-NYC Jan 2016 Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with Eclipse
 
Dagger 2 - Injeção de Dependência
Dagger 2 - Injeção de DependênciaDagger 2 - Injeção de Dependência
Dagger 2 - Injeção de Dependência
 
CDI: How do I ?
CDI: How do I ?CDI: How do I ?
CDI: How do I ?
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Test ng
Test ngTest ng
Test ng
 
Android MvRx Framework 介紹
Android MvRx Framework 介紹Android MvRx Framework 介紹
Android MvRx Framework 介紹
 
Android Loaders : Reloaded
Android Loaders : ReloadedAndroid Loaders : Reloaded
Android Loaders : Reloaded
 
Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
 
Introduction to TDD with FlexUnit
Introduction to TDD with FlexUnitIntroduction to TDD with FlexUnit
Introduction to TDD with FlexUnit
 

En vedette

Basic VR Development Tutorial Integrating Oculus Rift and Razer Hydra
Basic VR Development Tutorial Integrating Oculus Rift and Razer HydraBasic VR Development Tutorial Integrating Oculus Rift and Razer Hydra
Basic VR Development Tutorial Integrating Oculus Rift and Razer HydraChris Zaharia
 
UnityによるVR開発 - 入力デバイス編 -
UnityによるVR開発 - 入力デバイス編 -UnityによるVR開発 - 入力デバイス編 -
UnityによるVR開発 - 入力デバイス編 -Tomonori Takata
 
建築VRを作る方法 | SketchUp研究会 第4回定例会
建築VRを作る方法 | SketchUp研究会 第4回定例会建築VRを作る方法 | SketchUp研究会 第4回定例会
建築VRを作る方法 | SketchUp研究会 第4回定例会Tomonori Takata
 
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会Koji Hasegawa
 
Oculus Rift DK2 + Leap Motion Tutorial
Oculus Rift DK2 + Leap Motion TutorialOculus Rift DK2 + Leap Motion Tutorial
Oculus Rift DK2 + Leap Motion TutorialChris Zaharia
 
VRの入力デバイス #JAGVR
VRの入力デバイス #JAGVRVRの入力デバイス #JAGVR
VRの入力デバイス #JAGVRKoji Hasegawa
 
テストの種類とBDD #33testing
テストの種類とBDD #33testingテストの種類とBDD #33testing
テストの種類とBDD #33testingKoji Hasegawa
 
ビルドプロセスとCI #STAC2014
ビルドプロセスとCI #STAC2014ビルドプロセスとCI #STAC2014
ビルドプロセスとCI #STAC2014Koji Hasegawa
 

En vedette (8)

Basic VR Development Tutorial Integrating Oculus Rift and Razer Hydra
Basic VR Development Tutorial Integrating Oculus Rift and Razer HydraBasic VR Development Tutorial Integrating Oculus Rift and Razer Hydra
Basic VR Development Tutorial Integrating Oculus Rift and Razer Hydra
 
UnityによるVR開発 - 入力デバイス編 -
UnityによるVR開発 - 入力デバイス編 -UnityによるVR開発 - 入力デバイス編 -
UnityによるVR開発 - 入力デバイス編 -
 
建築VRを作る方法 | SketchUp研究会 第4回定例会
建築VRを作る方法 | SketchUp研究会 第4回定例会建築VRを作る方法 | SketchUp研究会 第4回定例会
建築VRを作る方法 | SketchUp研究会 第4回定例会
 
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会
Xcode 7におけるUIテストとカバレジ計測 #yidev 第20回勉強会
 
Oculus Rift DK2 + Leap Motion Tutorial
Oculus Rift DK2 + Leap Motion TutorialOculus Rift DK2 + Leap Motion Tutorial
Oculus Rift DK2 + Leap Motion Tutorial
 
VRの入力デバイス #JAGVR
VRの入力デバイス #JAGVRVRの入力デバイス #JAGVR
VRの入力デバイス #JAGVR
 
テストの種類とBDD #33testing
テストの種類とBDD #33testingテストの種類とBDD #33testing
テストの種類とBDD #33testing
 
ビルドプロセスとCI #STAC2014
ビルドプロセスとCI #STAC2014ビルドプロセスとCI #STAC2014
ビルドプロセスとCI #STAC2014
 

Similaire à Atec mtg7 unittest

Android Unit Test
Android Unit TestAndroid Unit Test
Android Unit TestPhuoc Bui
 
Mobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve MobileMobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve MobileKonstantin Loginov
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 
DevQuiz 2011 の模範解答 Android編
DevQuiz 2011 の模範解答 Android編DevQuiz 2011 の模範解答 Android編
DevQuiz 2011 の模範解答 Android編Makoto Yamazaki
 
Integration Group - Lithium test strategy
Integration Group - Lithium test strategyIntegration Group - Lithium test strategy
Integration Group - Lithium test strategyOpenDaylight
 
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, MelonUnit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, MelonbeITconference
 
Automated integration tests for ajax applications (с. карпушин, auriga)
Automated integration tests for ajax applications (с. карпушин, auriga)Automated integration tests for ajax applications (с. карпушин, auriga)
Automated integration tests for ajax applications (с. карпушин, auriga)Mobile Developer Day
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekPaco van Beckhoven
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in JavaAnkur Maheshwari
 
Android Building, Testing and reversing
Android Building, Testing and reversingAndroid Building, Testing and reversing
Android Building, Testing and reversingEnrique López Mañas
 
Testing and Building Android
Testing and Building AndroidTesting and Building Android
Testing and Building AndroidDroidcon Berlin
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKFabio Collini
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaFabio Collini
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testabilitydrewz lin
 
Successful Teams are TDD Teams
Successful Teams are TDD TeamsSuccessful Teams are TDD Teams
Successful Teams are TDD TeamsRob Myers
 
Improve unit tests with Mutants!
Improve unit tests with Mutants!Improve unit tests with Mutants!
Improve unit tests with Mutants!Paco van Beckhoven
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 

Similaire à Atec mtg7 unittest (20)

Android Unit Test
Android Unit TestAndroid Unit Test
Android Unit Test
 
Mobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve MobileMobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve Mobile
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Android testing
Android testingAndroid testing
Android testing
 
DevQuiz 2011 の模範解答 Android編
DevQuiz 2011 の模範解答 Android編DevQuiz 2011 の模範解答 Android編
DevQuiz 2011 の模範解答 Android編
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Integration Group - Lithium test strategy
Integration Group - Lithium test strategyIntegration Group - Lithium test strategy
Integration Group - Lithium test strategy
 
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, MelonUnit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
 
Robolectric Adventure
Robolectric AdventureRobolectric Adventure
Robolectric Adventure
 
Automated integration tests for ajax applications (с. карпушин, auriga)
Automated integration tests for ajax applications (с. карпушин, auriga)Automated integration tests for ajax applications (с. карпушин, auriga)
Automated integration tests for ajax applications (с. карпушин, auriga)
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech Week
 
Junit mockito and PowerMock in Java
Junit mockito and  PowerMock in JavaJunit mockito and  PowerMock in Java
Junit mockito and PowerMock in Java
 
Android Building, Testing and reversing
Android Building, Testing and reversingAndroid Building, Testing and reversing
Android Building, Testing and reversing
 
Testing and Building Android
Testing and Building AndroidTesting and Building Android
Testing and Building Android
 
Testing Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UKTesting Android apps based on Dagger and RxJava Droidcon UK
Testing Android apps based on Dagger and RxJava Droidcon UK
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability
 
Successful Teams are TDD Teams
Successful Teams are TDD TeamsSuccessful Teams are TDD Teams
Successful Teams are TDD Teams
 
Improve unit tests with Mutants!
Improve unit tests with Mutants!Improve unit tests with Mutants!
Improve unit tests with Mutants!
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 

Plus de Koji Hasegawa

Blender-like SceneView Hotkeys Unity extensions
Blender-like SceneView Hotkeys Unity extensionsBlender-like SceneView Hotkeys Unity extensions
Blender-like SceneView Hotkeys Unity extensionsKoji Hasegawa
 
IntelliJ plugin の作りかた
IntelliJ plugin の作りかたIntelliJ plugin の作りかた
IntelliJ plugin の作りかたKoji Hasegawa
 
AltUnityTesterを試してみた #gotandaunity
AltUnityTesterを試してみた #gotandaunityAltUnityTesterを試してみた #gotandaunity
AltUnityTesterを試してみた #gotandaunityKoji Hasegawa
 
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAA
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAAスマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAA
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAAKoji Hasegawa
 
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編Koji Hasegawa
 
iOSアプリ開発でもTravis CI #eytokyo
iOSアプリ開発でもTravis CI #eytokyoiOSアプリ開発でもTravis CI #eytokyo
iOSアプリ開発でもTravis CI #eytokyoKoji Hasegawa
 
テストフィクスチャTips(主にCoreData) #potatotips
テストフィクスチャTips(主にCoreData) #potatotipsテストフィクスチャTips(主にCoreData) #potatotips
テストフィクスチャTips(主にCoreData) #potatotipsKoji Hasegawa
 
Androidで使えるモックフレームワーク
Androidで使えるモックフレームワークAndroidで使えるモックフレームワーク
Androidで使えるモックフレームワークKoji Hasegawa
 
スマートフォンアプリの テスト自動化をはじめよう
スマートフォンアプリの テスト自動化をはじめようスマートフォンアプリの テスト自動化をはじめよう
スマートフォンアプリの テスト自動化をはじめようKoji Hasegawa
 
第3回Ques ここからはじめる!Androidアプリのテスト自動化
第3回Ques ここからはじめる!Androidアプリのテスト自動化第3回Ques ここからはじめる!Androidアプリのテスト自動化
第3回Ques ここからはじめる!Androidアプリのテスト自動化Koji Hasegawa
 
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっ
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっAndroidとの同時開発だけどモデルをC++で書けば問題ないよねっ
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっKoji Hasegawa
 
Testterチーム2011年まとめ
Testterチーム2011年まとめTestterチーム2011年まとめ
Testterチーム2011年まとめKoji Hasegawa
 
Testter単体テストのビアレビュー報告
Testter単体テストのビアレビュー報告Testter単体テストのビアレビュー報告
Testter単体テストのビアレビュー報告Koji Hasegawa
 
山吹色の茸疾走におけるテストの実例
山吹色の茸疾走におけるテストの実例山吹色の茸疾走におけるテストの実例
山吹色の茸疾走におけるテストの実例Koji Hasegawa
 

Plus de Koji Hasegawa (15)

Blender-like SceneView Hotkeys Unity extensions
Blender-like SceneView Hotkeys Unity extensionsBlender-like SceneView Hotkeys Unity extensions
Blender-like SceneView Hotkeys Unity extensions
 
IntelliJ plugin の作りかた
IntelliJ plugin の作りかたIntelliJ plugin の作りかた
IntelliJ plugin の作りかた
 
AltUnityTesterを試してみた #gotandaunity
AltUnityTesterを試してみた #gotandaunityAltUnityTesterを試してみた #gotandaunity
AltUnityTesterを試してみた #gotandaunity
 
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAA
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAAスマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAA
スマートフォンアプリ開発と自動化 〜なじむ。実に!なじむぞ!〜 #AsianAA
 
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編
スマートフォンアプリの色々自動化をはじめよう - at SonyDNA Meisters Salon 特別編
 
iOSアプリ開発でもTravis CI #eytokyo
iOSアプリ開発でもTravis CI #eytokyoiOSアプリ開発でもTravis CI #eytokyo
iOSアプリ開発でもTravis CI #eytokyo
 
テストフィクスチャTips(主にCoreData) #potatotips
テストフィクスチャTips(主にCoreData) #potatotipsテストフィクスチャTips(主にCoreData) #potatotips
テストフィクスチャTips(主にCoreData) #potatotips
 
Androidで使えるモックフレームワーク
Androidで使えるモックフレームワークAndroidで使えるモックフレームワーク
Androidで使えるモックフレームワーク
 
スマートフォンアプリの テスト自動化をはじめよう
スマートフォンアプリの テスト自動化をはじめようスマートフォンアプリの テスト自動化をはじめよう
スマートフォンアプリの テスト自動化をはじめよう
 
第3回Ques ここからはじめる!Androidアプリのテスト自動化
第3回Ques ここからはじめる!Androidアプリのテスト自動化第3回Ques ここからはじめる!Androidアプリのテスト自動化
第3回Ques ここからはじめる!Androidアプリのテスト自動化
 
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっ
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっAndroidとの同時開発だけどモデルをC++で書けば問題ないよねっ
Androidとの同時開発だけどモデルをC++で書けば問題ないよねっ
 
Testterチーム2011年まとめ
Testterチーム2011年まとめTestterチーム2011年まとめ
Testterチーム2011年まとめ
 
Testter単体テストのビアレビュー報告
Testter単体テストのビアレビュー報告Testter単体テストのビアレビュー報告
Testter単体テストのビアレビュー報告
 
山吹色の茸疾走におけるテストの実例
山吹色の茸疾走におけるテストの実例山吹色の茸疾走におけるテストの実例
山吹色の茸疾走におけるテストの実例
 
Testterを叩け!
Testterを叩け!Testterを叩け!
Testterを叩け!
 

Atec mtg7 unittest

  • 2. • Twitter id @nowsprinting • • • Java, Swing, Do-Ja, iOS SIer/ 2011 5 26
  • 3. Agenda • • JUnit • Android Testing Framework • Android Mock 2011 5 26
  • 4. 2011 5 26
  • 5. ©snsk at ATEC Mtg#1 2011 5 26
  • 6. ©snsk at ATEC Mtg#1 2011 5 26
  • 7. 2011 5 26
  • 8. Activity Http Lib (Mock) Lib (Mock) 2011 5 26
  • 9. Monkey God Hand Runner Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest 2011 5 26
  • 10. Android Activity Monkey → God Hand Runner →suspend→resume Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest 2011 5 26
  • 11. Monkey Instrumental God Hand Runner TestCase Activity Http Lib (Mock) Lib (Mock) Robolectric AndroidTest 2011 5 26
  • 12. JUnit • Android Testing Framework JUnit3 • TestCase • TestCase Assertion 2011 5 26
  • 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 TestCase 2011 5 26
  • 14. “test” + + public void testTwitterLogicAuthorization() { Authorization auth4test = ... TwitterLogic target = new TwitterLogic(auth4test); assertNotNull("Twitter ", target.twitter); } Assertion 2011 5 26
  • 16. 2011 5 26
  • 17. Android Testing Framework 2011 5 26
  • 19. eclipse package explorer New Test Project... 2011 5 26
  • 20. Dialog (1/2) TestProject Project 2011 5 26
  • 21. Dialog (2/2) package • • Class package 2011 5 26
  • 22. Why? 2011 5 26
  • 25. Android Testing Framework / • • Class package package 2011 5 26
  • 27. Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase 2011 5 26
  • 28. Test Cases (1/2) • AndroidTestCase • ApplicationTestCase • LoaderTestCase Testter • ProviderTestCase2 • ServiceTestCase ※ JUnit TestCase 2011 5 26
  • 29. Test Cases (2/2) • InstrumentationTestCase • ActivityTestCase • ActivityInstrumentationTestCase2 • ActivityUnitTestCase • SingleLaunchActivityTestCase • SyncBaseInstrumentation 2011 5 26
  • 31. AndroidTestCase • Activity • Context getContext() Context 2011 5 26
  • 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. ActivityInstrumentation TestCase2 2011 5 26
  • 34. ActivityInstrumentation TestCase2 • Activity • Activity • Context getContext() • ActivityInstrumentationTestCase @Deprecated 2011 5 26
  • 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. 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 res 2011 5 26
  • 37. public void testRegistBtnText() { // assertTrue(mButton.getText().toString() .equalsIgnoreCase(" ")); } Assertion 2011 5 26
  • 38. UI public void testClickButton() { mActivity.runOnUiThread(new Runnable() { public void run() { try { mButton.performClick(); assertXxx(...); } catch (RuntimeException e) { assertXxx(...); } } }); } 2011 5 26
  • 39. private 2011 5 26
  • 40. private private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) } 2011 5 26
  • 41. Class<AuthActivity> clazz = AuthActivity.class; Field field = clazz .getDeclaredField("preferenceLogic"); Method method = getClass() .getMethod("getCacheDrawable"); method.invoke(); 2011 5 26
  • 42. CV 2011 5 26
  • 43. Fragile Test private final PreferenceLogic preferenceLogic = PreferenceLogic.getInstance(); private Drawable getCacheDrawable(URL url) { (snip) } default Package Private 2011 5 26
  • 44. Class<View> clazz = View.class; Field field = clazz.getDeclaredField("mOnClickListener"); field.setAccessible(true); assertTrue(field.get(mButton) != null); AndroidSDK Class field → Mock 2011 5 26
  • 45. 2011 5 26
  • 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. fail try { target.auth(null); fail("Twitter#getOAuthAccessToken() "); } catch (RuntimeException e) { assertEquals("catch ", expected, e.getCause()); } 2011 5 26
  • 49. Android Mock • Dalvk EasyMock • Mock Class 2011 5 26
  • 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. 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. 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); } Mock 2011 5 26
  • 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
  • 55. • backlog wiki • 2011 5 26