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

Robolectric with PowerMock for Android Studio

17997dee8a3da090f62d8cf8c494d8ff?s=47 TOYAMA Sumio
September 24, 2014

Robolectric with PowerMock for Android Studio

「【第9回】potatotips(iOS/Android開発Tips共有会)」の発表資料です。
Android Studio / Gradle で、RobolectricとPowerMockを併用する方法を説明しています。
設定さえしてしまえば、Android StudioでサクサクTDDできますので、ぜひお試しください!

17997dee8a3da090f62d8cf8c494d8ff?s=128

TOYAMA Sumio

September 24, 2014
Tweet

Transcript

  1. ʙϨΨγʔίʔυͱಆ͏ͨΊʹʙ   RobolectricͰ΋PowerMockΛ࢖͏ 2014.09.24   @sumio_tym  (TOYAMA  Sumio) potatotips  (iOS/Android։ൃTipsڞ༗ձ)

     ୈ9ճ
  2. ࣗݾ঺հ •  ࢯ໊:  ֎ࢁ७ੜ(TOYAMA  Sumio)   @sumio_tym  (twitter),  @sumio  (github)

      •  ॴଐ:  NTTιϑτ΢ΣΞגࣜձࣾ   •  ۀ຿಺༰:   – ࣾ಺Androidؔ࿈ϓϩδΣΫτٕज़ࢧԉ   – Androidؔ࿈ࣾ಺ݚमߨࢣ   •  ϓϥΠϕʔτ:   – STAR  (ςετࣗಈԽݚڀձ)   – Androidͷࣗಈςετ༻ͷIMEΛެ։ͯ͠·͢   https://github.com/sumio/uiautomator-­‐unicode-­‐input-­‐helper   2  
  3. •  Androidͷ୯ମςετɺͲ͏ͯ͠·͔͢?   – Robolectric  +  Mockito   •  AndroidͰϨΨγʔίʔυͱಆΘͳ͖Ό͍ ͚ͳ͍͜ͱɺͳ͍Ͱ͔͢?

      •  staticϝιου΍finalϝιουΛࠩ͠ସ͑ ͨ͘ͳΔ͜ͱɺͳ͍Ͱ͔͢? 3   ͸͡Ίʹ
  4. ঺հ͢ΔTips RobolectricͰPowerMockΛ࢖͏ํ๏ Λ঺հ͠·͢(Android  Studioฤ) 4   ҙ֎ͱ໘౗!

  5. Robolectric •  JVM্ͰAndroidͷ୯ମςετ͕Ͱ͖Δ!   •  http://robolectric.org/ 5   ग़య:  http://robolectric.org/

  6. PowerMock  with  Mockito •  https://code.google.com/p/powermock/   •  staticɾfinalϝιου΋ࠩ͠ସ͑ΒΕΔ   6

      ग़య:  https://code.google.com/p/powermock/wiki/MockitoUsage13
  7. ΞδΣϯμ •  Robolectric+PowerMock:  ซ༻͕೉͍͠ཧ༝   •  ղܾํ਑   •  ϙΠϯτ

      •  ϑΝΠϧͷ഑ஔ   •  gradleରԠ   •  Android  Studioઃఆ   •  ·ͱΊ   7  
  8. Robolectric+PowerMock:   ซ༻͕೉͍͠ཧ༝ •  ྆ํͱ΋@RunWithࢦఆ͕ඞཁ   – Robolectric     – PowerMock

    8   @RunWith(RobolectricTestRunner.class) public class MyActivityTest { ... @RunWith(PowerMockRunner.class) public class YourTestCase { .... @RunWithͰࢦఆͰ͖Δςετϥϯφʔ͸1͚ͭͩ!  
  9. ղܾํ਑ •  @RunWith:  RobolectricTestRunnerΛࢦఆ   •  PowerMock:  PowerMockAgent   https://code.google.com/p/powermock/wiki/

    PowerMockAgent 9   @RunWith(RobolectricTestRunner.class) @PrepareForTest(X.class) public class MyActivityPowerMockTest extends { static { PowerMockAgent.initializeIfNeeded(); } ...
  10. ϙΠϯτ •  JDK1.6ඞਢ(JDK1.7Ҏ߱Ͱ͸ಈ࡞͠ͳ͍)   •  robolectric-­‐gradle-­‐pluginΛ࢖͏   – https://github.com/robolectric/robolectric-­‐gradle-­‐ plugin  (masterϒϥϯνͷઌ୺͕ඞཁ)

      •  ςετ࣮ߦ࣌ʹ޻෉͕ඞཁ   – Ϋϥεύε:  junit-­‐4.11Λandroid.jarΑΓલʹ!   – ىಈ࣌Ҿ਺:  -­‐javaagentΦϓγϣϯ͕ඞཁ   10   -javaagent:/path/to/powermock-module-javaagent-1.5.5.jar
  11. ϑΝΠϧͷ഑ஔ 11   src/test/javaʹςετίʔυΛ഑ஔ src/test/resʹ   RobolectricͷConfigϑΝΠϧΛ഑ஔ

  12. GradleରԠ(४උ) •  robolectric-­‐gradle-­‐pluginͷmasterઌ୺ΛϏ ϧυ͢Δ   – όʔδϣϯએݴมߋ     – gradle  install

      12   ... group = 'org.robolectric' version = '0.13.0-SNAPSHOT' ...
  13. GradleରԠ(build.gradle  1/4) 13   buildscript { repositories { mavenCentral() mavenLocal()

    } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+' } } apply plugin: 'android' apply plugin: 'robolectric' // ...ଓ͘... robolectric-­‐gradle-­‐plugin͸   ϩʔΧϧʹΠϯετʔϧͨ͠΋ͷΛ࢖͏ 'robolectric'ϓϥάΠϯΛద༻
  14. GradleରԠ(build.gradle  2/4) 14   android { ... sourceSets { androidTest

    { setRoot('src/test') } ... } } // ...ଓ͘... src/testʹςετίʔυΛஔ͘
  15. GradleରԠ(build.gradle  3/4) 15   dependencies { repositories { mavenCentral() }

    androidTestCompile 'org.powermock:powermock-module-junit4:1.5.5' androidTestCompile 'org.powermock:powermock-api-mockito:1.5.5' androidTestCompile 'org.powermock:powermock-module-junit4-rule-agent: 1.5.5' androidTestCompile('org.robolectric:robolectric:2.3') { exclude module: 'commons-logging' exclude module: 'httpclient' exclude module: 'support-v4' } } // ...ଓ͘... PowerMockAgentΛ௥Ճ
  16. GradleରԠ(build.gradle  4/4) 16   robolectric { ignoreFailures true def agentJar

    = configurations.androidTestCompile.files { dep -> (dep.group == 'org.powermock') && (dep.name == 'powermock-module-junit4-rule-agent') }.find { it.name.startsWith 'powermock-module-javaagent' } jvmArgs "-javaagent:${agentJar.absolutePath}" } powermock-­‐module-­‐javaagentͷjarϑΝΠϧͷॴࡏΛ୳ ͯ͠ɺjvmArgsʹηοτ͢Δ robolectric  {  ...  }͸dependencies  {  ...  }ΑΓ΋ޙʹએݴ͢Δ
  17. Android  Studioઃఆ(1/3) 17   •  Android͡Όͳ͍[All    Test]Λ૸Βࣦͤͯഊ͞ ͤΔ  

    •  ΤϥʔͱҰॹʹग़ྗ͞ΕΔίϚϯυϥΠϯ͔ ΒΫϥεύεΛద౰ͳΤσΟλʹίϐʔ    
  18. 18   •  -­‐classpathͷฤू   •  junit-­‐4.11.jarΛઌ಄ʹҠಈ   •  build/test-­‐classes΁ͷύεΛ຤ඌʹ௥Ճ

      •  powermock-­‐module-­‐javaagent-­‐1.5.5.jarΛ-­‐javaagentΦϓ γϣϯͱͯ͠௥Ճ   •  -­‐javaagent:/path/to/powermock-­‐module-­‐ javaagent-­‐1.5.5.jar
  19. Android  Studioઃఆ(2/3) 19   •  [Run]>[Edit  Configurations...]ͷ[JUnit]σϑΥ ϧτ஋Λมߋ   ฤूͨ͠΋ͷΛషΓ෇͚Δ

    ςετ࣮ߦલʹ   gradle  testDebugClasses   Λςετ࣮ߦ͢ΔΑ͏ʹઃఆ͢Δ
  20. Android  Studioઃఆ(3/3) •  ςετ͍ͨ͠ϝιου΍ΫϥεͰҎԼͷૢ ࡞Λ͢Ε͹ςετ͕࣮ߦͰ͖Δ   – ӈΫϦοΫˠRunˠAndroidͰ͸ͳ͍ํͷςετ 20   ͜ͷΞΠίϯͷํΛબͿ

  21. ·ͱΊ •  AndroidStudioͰɺRobolectricͱPowerMockΛซ༻͢ Δํ๏Λ঺հ͠·ͨ͠ɻ   •  PowerMockΛ࢖Θ͟ΔΛಘͳ͍ϨΨγʔίʔυʹ ૺ۰ͨ͠ͱ͖ʹ͓࢖͍͍ͩ͘͞ɻ   • 

    ॳظઃఆ͑͞ऴΘΕ͹ɺշదʹ࢖͑·͢!   21   ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠
  22. ࢀߟURL •  robolectric-­‐gradle-­‐plugin   –  https://github.com/robolectric/robolectric-­‐gradle-­‐plugin   •  Robolectricར༻࣌ͷbuild.gradleͷαϯϓϧ  

    –  https://github.com/robolectric/deckard-­‐gradle   •  Android  StudioͰRobolectricͷςετΛ࣮ߦ͢Δ ํ๏Λղઆͨ͠ϒϩάهࣄ   –  ʮAndroid  Studio͔ΒRobolectricͷςετΛ௚઀࣮ߦ͢ Δ-­‐  ΄͛΄͛ʢԾʣʯhttp://starzero.hatenablog.com/ entry/2014/05/13/194422 22