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

Android Testing Bootcamp #3 Hands-On

Android Testing Bootcamp #3 Hands-On

Android Testing Bootcamp #3 Hands-On 資料

3cca191bf3064fd059ea2c3d6022afbd?s=128

Fumihiko Shiroyama

September 09, 2016
Tweet

Transcript

  1. "OESPJE5FTUJOH#PPUDBNQ )BOET0O "OESPJE@5FTUJOH@#$

  2. w "OESPJE5FTUJOH#PPUDBNQ(SPVQ w IUUQBOESPJEUFTUJOH CPPUDBNQDPOOQBTTDPN w "OESPJE5FTUJOH#PPUDBNQ w IUUQBOESPJEUFTUJOH CPPUDBNQDPOOQBTTDPNFWFOU

  3. ࠓճར༻͢Δ؀ڥ

  4. w "OESPJE4UVEJP w IUUQTHJUIVCDPNTSZN5FTU)BOET0O w HJUDMPOFHJU!HJUIVCDPNTSZN 5FTU)BOET0OHJU w HJUDPFYBNQMFྫ୊ϒϥϯν w

    HJUDPBOTXFSղ౴ྫϒϥϯν
  5. ΞδΣϯμ

  6. w ͳͥςετΛॻ͘ͷ͔ w ςετͷछྨ w +6OJU w .PDLJUP w 1PXFS.PDL

    w %PHHFS 6*ςετ͸ ࣍ճϋϯζΦϯΛ ָ͓͠Έʹʂ
  7. ͳͥςετΛॻ͘ͷ͔

  8. w ϩδοΫͷݕূ w ࢓༷มߋ΁ͷ௥ै w σάϨͷݕग़ w ઃܭͷݟ௚͠ͷ͖͔͚ͬ

  9. ςετͷछྨ

  10. w ୯ମςετ 6OJU5FTU  w ݁߹ςετ *OUFHSBUJPO5FTU  w Ϣʔβड͚ೖΕςετ

    6TFS"DDFQUBODF5FTU  w ͦͷଞɺෛՙςετɾηΩϡϦςΟςετ౳
  11. 6OJU5FTU w ϓϩάϥϚ͕࣮૷ w ϝιου΍ΫϥεʹରʹରԠ w ࣗಈ࣮ߦ w ߴ଎ w

    "OESPJEͷ৔߹ɺ࣮ػ؀ڥͰͳ͍͜ͱ͕ଟ͍
  12. *OUFHSBUJPO5FTU w 6*ʹର͢ΔࣗಈςετΛؚΉ w ࣮ࡍͷը໘ભҠ΍*0ॲཧɺωοτϫʔΫ௨৴Λߦ ͏͜ͱ΋ଟ͍ w ࣮ػʢΤϛϡϨʔλؚΉʣ࣮ߦ ఆٛ΍ӡ༻͸ ݱ৔ʹΑͬͯҧ͏

  13. ड͚ೖΕςετ w ൃ஫ݩɺϚωʔδϟɾσΟϨΫλ͕֬ೝ w 2"͞ΜɺςελʹΑΔ֬ೝ w ଟ͘ͷ৔߹खಈ ఆٛ΍ӡ༻͸ ݱ৔ʹΑͬͯҧ͏

  14. ͦͷଞ w ෛՙςετ w ηΩϡϦςΟςετ w ͦͷଞॾʑ

  15. +6OJU

  16. w "OESPJEͷ6OJU5FTUͷσϑΝΫτελϯμʔυ w "OESPJE4UVEJPҎ߱Λ࢖͍ͬͯΕ͹Կ΋͠ͳ͘ ͯ΋ར༻Մೳ w Ξϊςʔγϣϯϕʔε w +7.؀ڥͷΈͰಈ࡞͠ඇৗʹߴ଎

  17. CVJMEHSBEMF EFQFOEFODJFT\ DPNQJMFpMF5SFF EJSMJCT JODMVEF< KBS>  UFTU$PNQJMFKVOJUKVOJU DPNQJMFDPNBOESPJETVQQPSUBQQDPNQBUW ^

  18. +6OJUͷجຊ

  19. w NPEVMFTSDUFTUKBWBQBDLBHF5BSHFU5FTUKBWB w !#FGPSFʜॳظԽ w !"GUFSʜޙॲཧ w !5FTUʜςετίʔυຊମ

  20. public class CalculatorTest {
 private Calculator calculator;
 
 @Before
 public

    void setUp() {
 calculator = new Calculator();
 }
 
 @After
 public void tearDown() {
 
 }
 
 @Test
 public void ଍͠ࢉ͕ਖ਼͘͠ग़དྷΔ() throws Exception {
 double expected = calculator.add(1.0, 2.0);
 assertTrue(expected == 3.0);
 }
 }
  21. "TTFSUJPO w "TTFSUBTTFSU5SVF CPPMFBODPOEJUJPO  w "TTFSUBTTFSU&RVBMT 0CKFDUFYQFDUFE 0CKFDU BDUVBM

  22. ՝୊ w  )FMMP+6OJUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFKVOJUTSDUFTUKBWBVTTIJSPZBNBBOESPJE KVOJU)FMMP+6OJUKBWB w 

    $BMDVMBUPS5FTUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFKVOJUTSDUFTUKBWBVTTIJSPZBNBBOESPJE KVOJU$BMDVMBUPS5FTUKBWB
  23. .BUDIFS w .BUDIFSʜʮͲΜͳঢ়ଶ͔ʯΛද͢ w "TTFSUBTTFSU5IBU BDUVBM NBUDIFS  w )BNDSFTUͷ.BUDIFST͕༗໊

    w IUUQIBNDSFTUPSH+BWB)BNDSFTU
  24. Assert.assertThat(1 + 1, Matchers.is(2));
 assertThat("MIKE", is(not("STEVE")));
 assertThat(1000L, is(instanceOf(Integer.class)));

  25. EFQFOEFODJFT\ UFTU$PNQJMFKVOJUKVOJU UFTU$PNQJMFPSHIBNDSFTUIBNDSFTUMJCSBSZ ^

  26. Α͘࢖͏.BUDIFS w JT ʜಉ஋ w OPU ʜ൱ఆ w OVMM7BMVF OPU/VMM7BMVF

    ʜ/VMMʗඇ/VMM w TBNF*OTUBODF ʜಉҰΠϯελϯε w JOTUBODF0G ʜಉҰ͔αϒΫϥεͷΠϯελϯε w TUBSUT8JUI FOET8JUI ʜࢦఆจࣈྻͰ࢝·ΔʗऴΘΔ w DPOUBJOT4USJOH ʜࢦఆจࣈྻؚ͕·ΕΔ w JT&NQUZ4USJOH ʜۭจࣈྻ
  27. w HSFBUFS5IBO HSFBUFS5IBO0S&RVBM5P ʜΑΓେ͖͍ʗҎ ্ w MFTT5IBO MFTT5IBO0S&RVBM5P ʜΑΓখ͍͞ʗҎԼ w

    DMPTF5P ʜࢦఆ਺஋ͷൣғ಺͔Ͳ͏͔ w BTTFSU5IBU  JT DMPTF5P   ͸ʙൣ ғ಺ͳͷͰUSVF w ͦͷଞɺϦετ΍Ϛοϓ༻ͷ.BUDIFS΋ଟ਺ w ࢀߟ IUUQRJJUBDPNPQFOHMJUFNT FEBCFGBB
  28. ՝୊ w  $BMDVMBUPS5FTU8JUI)BNDSFTU.BUDIFSTΛ׬ ੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFKVOJUTSDUFTUKBWBVTTIJSPZBNB BOESPJEKVOJU $BMDVMBUPS5FTU8JUI)BNDSFTU.BUDIFSTKBWB

  29. .PDLJUP

  30. w ϝιουΛ͋Β͔͡ΊܾΊΒΕͨ݁Ռ͕ฦΔΑ͏ʹ ϞοΫΞοϓ͢Δ w *0ɺωοτϫʔΫΞΫηεɺϥϯμϜੑͷഉআ w ͦͷ৔Ͱςετ͍߲ͨ͠໨ʹूத͢Δ w .PDLJUP͕σϑΝΫτελϯμʔυ

  31. public class RandomIntGenerator {
 private final Random random = new

    Random();
 
 public int generate() {
 return random.nextInt(Integer.MAX_VALUE);
 }
 }
  32. public class RandomIntGeneratorTest {
 private RandomIntGenerator randomIntGenerator;
 
 @Before
 public

    void setUp() {
 randomIntGenerator = new RandomIntGenerator();
 }
 
 @After
 public void tearDown() {
 
 }
 
 @Test
 public void generateͷ݁Ռ() {
 int expected = randomIntGenerator.generate();
 assertThat(expected, is(1)); // ͜Μͳ͜ͱ͸Ͱ͖ͳ͍ʂʂ
 }
 }
  33. ࢖͍ํ RandomIntGenerator randomIntGenerator = Mockito.mock(RandomIntGenerator.class);
 Mockito.when(randomIntGenerator.generate()).thenReturn(123);

  34. w ଞʹ΋!.PDLΞϊςʔγϣϯΛ࢖͏΍Γํ͕͋Δ

  35. 
 @Mock
 RandomIntGenerator randomIntGeneratorWithAnnotation;
 
 @Before
 public void setUp() {


    MockitoAnnotations.initMocks(this);
 Mockito.when(randomIntGeneratorWithAnnotation.generate()).thenReturn(456);
 }
  36. ՝୊ w  3BOEPN*OU(FOFSBUPS5FTUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFNPDLJUPTSDUFTUKBWBVTTIJSPZBNB BOESPJENPDLJUP 3BOEPN*OU(FOFSBUPS5FTUKBWB w

     !.PDLΞϊςʔγϣϯΛ࢖͏ํ๏΋ࢼͯ͠ΈΑ ͏ʂ
  37. .PDLJUPWFSJGZ

  38. w .PDLΦϒδΣΫτ͸ɺݺͼग़͠Λ͢΂ͯهԱͯ͠ ͍Δ w ݺͼग़͞Εͨϝιουɺճ਺ɺҾ਺ͳͲΛ .PDLJUPWFSJGZͰݕূͰ͖Δ

  39. Mockito.verify(mockStrings, Mockito.times(1)).add("FOO");
 Mockito.verify(mockStrings, Mockito.atLeast(1)).add("BAR");
 Mockito.verify(mockStrings).add("BAZ");
 Mockito.verify(mockStrings, Mockito.never()).add("I AM NOT CALLED");

  40. EFQFOEFODJFT\ UFTU$PNQJMFKVOJUKVOJU UFTU$PNQJMFPSHIBNDSFTUIBNDSFTUMJCSBSZ UFTU$PNQJMFPSHNPDLJUPNPDLJUPDPSF ^

  41. .PDLJUPTQZ

  42. w ΦϒδΣΫτΛͦͷ··ϞοΫΞοϓ͢Δ .PDLJUPNPDLʹର͠ɺTQZ͸࣮ΦϒδΣΫτͷҰ ෦ͷΈΛࠩ͠ସ͑ΒΕΔ w TQZΦϒδΣΫτ͸NPDLͱಉ༷WFSJGZͰݕূͰ͖Δ w TQZͰ஋Λฦ͢ͱ͖͸ XIFO NPDL

    UIFO3FUVSO WBMVF Ͱ͸ͳ͘ EP3FUVSO WBMVF XIFO NPDL NFUIPE Λ࢖͏ w !.PDLಉ༷ɺ!4QZΞϊςʔγϣϯ͕ར༻Մೳ
  43. List<String> realList = new ArrayList<>();
 List<String> spyList = spy(realList);
 doReturn(38).when(spyList).size();

  44. ϑΟʔϧυʹ.PDLΛ ૹΓ͜Ή

  45. public class StringCollector {
 private List<String> strings = new ArrayList<>();


    
 public void add(@NonNull String string) {
 strings.add(string);
 }
 
 public int size() {
 return strings.size();
 }
 }
  46. w ϑΟʔϧυͷ"SSBZ-JTUʹͲ͏΍ͬͯϞοΫΛૹΓ ࠐΉʁ w  TFUUFSΛ༻ҙͯ͠!7JTJCMF'PS5FTUJOH w  !*OKFDU.PDLTΞϊςʔγϣϯ

  47. public class StringCollector {
 private List<String> strings = new ArrayList<>();


    
 public void add(@NonNull String string) {
 strings.add(string);
 }
 
 public int size() {
 return strings.size();
 }
 
 @VisibleForTesting
 void setStrings(List<String> strings) {
 this.strings = strings;
 }
 }
  48. !*OKFDU.PDLT @Mock
 List<String> mockStringsForInject;
 
 @InjectMocks
 StringCollector stringCollectorForInject;

  49. ՝୊ w  4USJOH$PMMFDUPS5FTUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFNPDLJUPTSDUFTUKBWBVTTIJSPZBNB BOESPJENPDLJUP4USJOH$PMMFDUPS5FTUKBWB w 

    !4QZΞ ϊςʔγϣϯΛ࢖͏ํ๏΋ࢼͯ͠ΈΑ͏ʂ w  !*OKFDU.PDLTΞϊςʔγϣϯ΋࢖ͬͯΈΑ͏ʂ
  50. .PDLJUPࢀߟϦϯΫ w IUUQFUDIBUFOBCMPHDPNFOUSZ  w IUUQFUDIBUFOBCMPHDPNFOUSZ  w IUUQFUDIBUFOBCMPHDPNFOUSZ 

  51. 1PXFS.PDL

  52. w ࠓճ͸͕࣌ؒͳ͍ͷͰ৮Ε·ͤΜ w pOBMDMBTT΍TUBUJDNFUIPEΛϞοΫͰ͖Δ w Ϋϥεϩʔμʹհೖ͢ΔͷͰϋϚϦϙΠϯτ͕͍ͬ ͺ͍͋Δ w ಟʹ΋ༀʹ΋ͳΓ͏Δ

  53. ςετ͠΍͍͢ߏ଄Խ

  54. w DPNNPOϞδϡʔϧͷ6TFS*OGP6TFS$BTFͷղઆ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFDPNNPOTSDNBJOKBWBVT TIJSPZBNBBOESPJEDPNNPOVTFDBTF 6TFS*OGP6TF$BTF*NQMKBWB

  55. ՝୊ w  6TFS*OGP6TF$BTF*NQM5FTUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFNPDLJUPTSDUFTUKBWBVTTIJSPZBNB BOESPJENPDLJUP 6TFS*OGP6TF$BTF*NQM5FTUKBWB w

    ඇಉظॲཧͷςετͷώϯτɿ$PVOU%PXO-BUDI
  56. w ςετΛͳΔ΂͘͠΍͍͢ߏ੒ʹ͢Δ w ద੾ʹΠϯλϑΣʔεΛ੾Δ w ґଘੑΛίϯετϥΫλͰ౉͢ w %*ίϯςφΛ࢖͏

  57. %BHHFS

  58. w (PPHMF͕ϝϯςφϯε͍ͯ͠Δ%*ίϯςφ w IUUQHPPHMFHJUIVCJPEBHHFS w .PEVMFͱ$PNQPOFOUΛ࢖ͬͯґଘੑͷάϥϑΛ ఆٛ͠ɺ!*OKFDUΞϊςʔγϣϯͰ࣮Πϯελϯε Λࠩ͠ࠐΉ w ςετ࣌ʹΠϯελϯεΛ੾Γସ͑ΒΕΔͳͲॊೈ

    ੑ͕૿͢
  59. @Module
 public class ApplicationModule {
 @Singleton
 @Provides
 public MessageHelper provideMessageHelper(Context

    context) {
 return MessageHelper.getInstance(context);
 }
 
 }
  60. @Singleton
 @Component(modules = ApplicationModule.class)
 public interface ApplicationComponent {
 ActivityComponent activityComponent(ActivityModule

    module);
 }
  61. public ApplicationComponent getComponent() {
 return applicationComponent;
 }
 
 @Override
 public

    void onCreate() {
 super.onCreate();
 
 applicationComponent = DaggerApplicationComponent.builder()
 .applicationModule(new ApplicationModule(this))
 .build(); }
  62. public class BaseActivity extends AppCompatActivity {
 private ActivityComponent activityComponent;
 


    /**
 * {@link android.app.Activity}ͷґଘੑάϥϑΛฦ͢
 *
 * @return
 */
 protected ActivityComponent getComponent() {
 if (activityComponent == null) {
 MyApplication myApplication = (MyApplication) getApplication();
 activityComponent = myApplication.getComponent().activityComponent(new ActivityModule(this));
 }
 return activityComponent;
 }
 }
  63. public class ChatActivity extends BaseActivity {
 
 @Inject
 MessageHelper messageHelper;

    // ҎԼུ }
  64. w %BHHFSΛ࢖࣮ͬͨફతͳྫ͸OVNBࢯʹҾ͖ܧ͗ ·͢ʂ