Save 37% off PRO during our Black Friday Sale! »

Annotationを利用した現在時刻のテスト

A051bd5f00287a5a332a146b04f14595?s=47 えぐ
May 10, 2017
780

 Annotationを利用した現在時刻のテスト

Android Testing Bootcamp #6 で発表した資料です

A051bd5f00287a5a332a146b04f14595?s=128

えぐ

May 10, 2017
Tweet

Transcript

  1. AnnotationΛ༻͍ͨ ݱࡏ࣌ࠁͷςετ Android Testing Bootcamp #6 2017/5/10

  2. ࣗݾ঺հ • ֎ࢁ ࣏༝ (@duane0728) • Ϡϑʔגࣜձࣾ

  3. ݱࡏ࣌ࠁΛ࢖ͬͨίʔυͷ ϢχοτςετͲ͏ॻ͍ͯ·͔͢ʁ

  4. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = System.currentTimeMillis();
 long timestamp = model.timestamp();
 
 int diff = 1; // do something
 return diff + "೔ޙ";
 }
 }
  5. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = System.currentTimeMillis();
 long timestamp = model.timestamp();
 
 int diff = 1; // do something
 return diff + "೔ޙ";
 }
 }
  6. Ұൠతʹߟ͑ΒΕΔ΍Γํ

  7. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ϓϩμΫγϣϯίʔυʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετର৅Ϋϥεͷґଘؔ܎͕૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳͯ͘͸͍͚ͳ͍

  8. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ɹɹ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  9. class Foo {
 
 String getRelativeTime(Model model, long now) {


    // do something
 }
 }
  10. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  11. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  12. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = System.currentTimeMillis();
 }
 
 @VisibleForTesting String getRelativeTime(Model model, long now) {
 }
 }
  13. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  14. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  15. class Foo {
 
 private final NowProvider provider;
 
 @Inject

    Foo(NowProvider provider) {
 this.provider = provider;
 }
 
 String getRelativeTime(Model model) {
 long now = provider.get();
 }
 } class NowProvider {
 long get() {
 return System.currentTimeMillis();
 }
 }
  16. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετର৅Ϋϥεͷґଘؔ܎͕૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  17. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετର৅Ϋϥεͷґଘؔ܎͕૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹ

  18. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = now();
 }
 
 // ςετ࣌ʹpartial mockԽ
 @VisibleForTesting long now() {
 return System.currentTimeMillis();
 }
 }
  19. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετର৅Ϋϥεͷґଘؔ܎͕૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳͯ͘͸͍͚ͳ͍

  20. 1.ɹϝιουͷҾ਺ʹ౉͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετର৅Ϋϥεͷґଘؔ܎͕૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετର৅Ϋϥε಺ʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳͯ͘͸͍͚ͳ͍

  21. ͲΕ΋Ұ௕Ұ୹

  22. YukiMatsumura / Now https://github.com/YukiMatsumura/Now

  23. Now • ݱࡏ࣌ࠁΛฦ͢staticͳϝιουΛ΋ͭΫϥεΛ༻ҙ͢Δ • ݱࡏ࣌ࠁΛ࢖͍͍ͨΫϥε͸ɺ͜ͷstaticϝιουΛ࢖͏ • ςετ࣌ʹAnnotationΛ࢖ͬͯɺݱࡏ࣌ࠁΛࢦఆͰ͖Δ ৄ͘͠஌Γ͍ͨํ͸Github΁ʂ

  24. public class Jsr310Time {
 
 private static Clock clock =

    Clock.systemUTC();
 
 public static long now() {
 return clock.millis();
 }
 
 // ...
 }
  25. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = Jsr310Time.now();
 }
 
 }
  26. public class FooTest {
 
 @Rule Jsr310TimeRule rule = new

    Jsr310TimeRule();
 
 @Now("2017-05-01T00:00:00Z")
 @Test public void test() {
 
 }
 }
  27. ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετର৅Ϋϥεͷґଘؔ܎͕૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍

  28. ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετର৅Ϋϥεͷґଘؔ܎͕૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍ ISO8601Ͱͷهड़͕ͪΐͬͱͭΒ͍ (ݸਓతʹ)

  29. Now with Kotlin

  30. Now with Kotlin • Kotlin͸AnnotationʹίϯετϥΫλΛએݴͰ͖Δ • ͜ͷػೳΛ࢖ͬͯɺISO8061Ͱ͸ͳ͘intܕͰ೔෇Λද͢ https://github.com/YukiMatsumura/Now#kotlin

  31. @Target(AnnotationTarget.FUNCTION)
 annotation class Now(
 val year: Int,
 val month: Int,


    val dayOfMonth: Int,
 val hour: Int,
 val minute: Int,
 val second: Int = 0
 ) https://kotlinlang.org/docs/reference/annotations.html#constructors
  32. class FooTest {
 
 @Rule @JvmField val rule = Jsr310TimeRule()


    
 @Now(2017, 5, 1, 0, 0, 0)
 @Test
 fun test() {
 
 }
 }
  33. ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετର৅Ϋϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετର৅Ϋϥεͷґଘؔ܎͕૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍ ೔࣌ΛIntͰදݱͰ͖ɺannotate͠΍͘͢ɺಡΈ΍͍͢(ݸਓతʹ)

  34. ·ͱΊ • ݱࡏ࣌ࠁʹґଘͨ͠ςετͷΞϓϩʔν͸ෳ਺ • ͲΕ΋Ұ௕Ұ୹ • AnnotationΛ༻͍ͨํ๏͸ັྗతͳΞϓϩʔν ͥͻݕ౼ͯ͠Έ͍ͯͩ͘͞

  35. ͋Γ͕ͱ͏͍͟͝·ͨ͠