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

Robolectric with PowerMock for Android Studio

TOYAMA Sumio
September 24, 2014

Robolectric with PowerMock for Android Studio

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

TOYAMA Sumio

September 24, 2014
Tweet

More Decks by TOYAMA Sumio

Other Decks in Programming

Transcript

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

      •  ॴଐ:  NTTιϑτ΢ΣΞגࣜձࣾ   •  ۀ຿಺༰:   – ࣾ಺Androidؔ࿈ϓϩδΣΫτٕज़ࢧԉ   – Androidؔ࿈ࣾ಺ݚमߨࢣ   •  ϓϥΠϕʔτ:   – STAR  (ςετࣗಈԽݚڀձ)   – Androidͷࣗಈςετ༻ͷIMEΛެ։ͯ͠·͢   https://github.com/sumio/uiautomator-­‐unicode-­‐input-­‐helper   2  
  2. ΞδΣϯμ •  Robolectric+PowerMock:  ซ༻͕೉͍͠ཧ༝   •  ղܾํ਑   •  ϙΠϯτ

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

    8   @RunWith(RobolectricTestRunner.class) public class MyActivityTest { ... @RunWith(PowerMockRunner.class) public class YourTestCase { .... @RunWithͰࢦఆͰ͖Δςετϥϯφʔ͸1͚ͭͩ!  
  4. ղܾํ਑ •  @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(); } ...
  5. ϙΠϯτ •  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
  6. 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'ϓϥάΠϯΛద༻
  7. GradleରԠ(build.gradle  2/4) 14   android { ... sourceSets { androidTest

    { setRoot('src/test') } ... } } // ...ଓ͘... src/testʹςετίʔυΛஔ͘
  8. 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Λ௥Ճ
  9. 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  {  ...  }ΑΓ΋ޙʹએݴ͢Δ
  10. Android  Studioઃఆ(1/3) 17   •  Android͡Όͳ͍[All    Test]Λ૸Βࣦͤͯഊ͞ ͤΔ  

    •  ΤϥʔͱҰॹʹग़ྗ͞ΕΔίϚϯυϥΠϯ͔ ΒΫϥεύεΛద౰ͳΤσΟλʹίϐʔ    
  11. 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
  12. ࢀߟ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