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 資料

Fumihiko Shiroyama

September 09, 2016
Tweet

More Decks by Fumihiko Shiroyama

Other Decks in Technology

Transcript

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

    w %PHHFS 6*ςετ͸ ࣍ճϋϯζΦϯΛ ָ͓͠Έʹʂ
  2. w ୯ମςετ 6OJU5FTU  w ݁߹ςετ *OUFHSBUJPO5FTU  w Ϣʔβड͚ೖΕςετ

    6TFS"DDFQUBODF5FTU  w ͦͷଞɺෛՙςετɾηΩϡϦςΟςετ౳
  3. 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);
 }
 }
  4. ՝୊ w  )FMMP+6OJUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFKVOJUTSDUFTUKBWBVTTIJSPZBNBBOESPJE KVOJU)FMMP+6OJUKBWB w 

    $BMDVMBUPS5FTUΛ׬੒ͤ͞Α͏ʂ w IUUQTHJUIVCDPNTSZN5FTU)BOET0OCMPC FYBNQMFKVOJUTSDUFTUKBWBVTTIJSPZBNBBOESPJE KVOJU$BMDVMBUPS5FTUKBWB
  5. Α͘࢖͏.BUDIFS w JT ʜಉ஋ w OPU ʜ൱ఆ w OVMM7BMVF OPU/VMM7BMVF

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

    DMPTF5P ʜࢦఆ਺஋ͷൣғ಺͔Ͳ͏͔ w BTTFSU5IBU  JT DMPTF5P   ͸ʙൣ ғ಺ͳͷͰUSVF w ͦͷଞɺϦετ΍Ϛοϓ༻ͷ.BUDIFS΋ଟ਺ w ࢀߟ IUUQRJJUBDPNPQFOHMJUFNT FEBCFGBB
  7. public class RandomIntGenerator {
 private final Random random = new

    Random();
 
 public int generate() {
 return random.nextInt(Integer.MAX_VALUE);
 }
 }
  8. 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)); // ͜Μͳ͜ͱ͸Ͱ͖ͳ͍ʂʂ
 }
 }
  9. 
 @Mock
 RandomIntGenerator randomIntGeneratorWithAnnotation;
 
 @Before
 public void setUp() {


    MockitoAnnotations.initMocks(this);
 Mockito.when(randomIntGeneratorWithAnnotation.generate()).thenReturn(456);
 }
  10. public class StringCollector {
 private List<String> strings = new ArrayList<>();


    
 public void add(@NonNull String string) {
 strings.add(string);
 }
 
 public int size() {
 return strings.size();
 }
 }
  11. 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;
 }
 }
  12. public ApplicationComponent getComponent() {
 return applicationComponent;
 }
 
 @Override
 public

    void onCreate() {
 super.onCreate();
 
 applicationComponent = DaggerApplicationComponent.builder()
 .applicationModule(new ApplicationModule(this))
 .build(); }
  13. 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;
 }
 }