Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Annotationを利用した現在時刻のテスト
Search
えぐ
May 10, 2017
0
830
Annotationを利用した現在時刻のテスト
Android Testing Bootcamp #6 で発表した資料です
えぐ
May 10, 2017
Tweet
Share
More Decks by えぐ
See All by えぐ
KotshiからMoshi-codegenに 乗り換えた経緯
egugue
0
2.2k
エンジニアにしてもよいこと エンジニアからお願いしたいこと
egugue
0
170
コードで事前条件を表明する
egugue
3
3.3k
Backends for Frontends を適用した話
egugue
1
1.1k
FlexboxLayoutの紹介
egugue
2
300
Robolectricの @Configを共通化する方法
egugue
0
810
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
128
8.8k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
24
600
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
Being A Developer After 40
akosma
83
590k
A better future with KSS
kneath
235
17k
Ruby is Unlike a Banana
tanoku
96
11k
BBQ
matthewcrist
83
9.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
80
5.1k
[RailsConf 2023] Rails as a piece of cake
palkan
46
4.6k
How GitHub Uses GitHub to Build GitHub
holman
472
290k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
23
1.7k
How to Ace a Technical Interview
jacobian
275
23k
Transcript
AnnotationΛ༻͍ͨ ݱࡏ࣌ࠁͷςετ Android Testing Bootcamp #6 2017/5/10
ࣗݾհ • ֎ࢁ ࣏༝ (@duane0728) • Ϡϑʔגࣜձࣾ
ݱࡏ࣌ࠁΛͬͨίʔυͷ ϢχοτςετͲ͏ॻ͍ͯ·͔͢ʁ
class Foo { String getRelativeTime(Model model) { long now
= System.currentTimeMillis(); long timestamp = model.timestamp(); int diff = 1; // do something return diff + "ޙ"; } }
class Foo { String getRelativeTime(Model model) { long now
= System.currentTimeMillis(); long timestamp = model.timestamp(); int diff = 1; // do something return diff + "ޙ"; } }
Ұൠతʹߟ͑ΒΕΔΓํ
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ϓϩμΫγϣϯίʔυʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετରΫϥεͷґଘ͕ؔ૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳ͍͚ͯ͘ͳ͍
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ɹɹ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
class Foo { String getRelativeTime(Model model, long now) {
// do something } }
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
class Foo { String getRelativeTime(Model model) { long now
= System.currentTimeMillis(); } @VisibleForTesting String getRelativeTime(Model model, long now) { } }
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
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(); } }
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετରΫϥεͷґଘ͕ؔ૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετରΫϥεͷґଘ͕ؔ૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹ
class Foo { String getRelativeTime(Model model) { long now
= now(); } // ςετ࣌ʹpartial mockԽ @VisibleForTesting long now() { return System.currentTimeMillis(); } }
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετରΫϥεͷґଘ͕ؔ૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳ͍͚ͯ͘ͳ͍
1.ɹϝιουͷҾʹ͢Α͏ʹ͢Δ ΧϓηϧԽ͕ଛͳΘΕΔʢέʔεʹΑΔʣ 2.ɹςετઐ༻ͷϝιουΛ༻ҙ͢Δ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖͬͯ͠·͏ 3.ɹݱࡏ࣌ࠁΛฦ͢ઐ༻ΫϥεΛ༻ҙ͢Δ ɹςετରΫϥεͷґଘ͕ؔ૿͑Δ 4.ɹݱࡏ࣌ࠁΛฦ͢ϝιουΛςετରΫϥεʹ༻ҙ͢Δ ɹݱࡏ࣌ࠁ͕ඞཁͳΫϥεશͯʹ༻ҙ͠ͳ͍͚ͯ͘ͳ͍
ͲΕҰҰ
YukiMatsumura / Now https://github.com/YukiMatsumura/Now
Now • ݱࡏ࣌ࠁΛฦ͢staticͳϝιουΛͭΫϥεΛ༻ҙ͢Δ • ݱࡏ࣌ࠁΛ͍͍ͨΫϥεɺ͜ͷstaticϝιουΛ͏ • ςετ࣌ʹAnnotationΛͬͯɺݱࡏ࣌ࠁΛࢦఆͰ͖Δ ৄ͘͠Γ͍ͨํGithubʂ
public class Jsr310Time { private static Clock clock =
Clock.systemUTC(); public static long now() { return clock.millis(); } // ... }
class Foo { String getRelativeTime(Model model) { long now
= Jsr310Time.now(); } }
public class FooTest { @Rule Jsr310TimeRule rule = new
Jsr310TimeRule(); @Now("2017-05-01T00:00:00Z") @Test public void test() { } }
ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετରΫϥεͷґଘ͕ؔ૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍
ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετରΫϥεͷґଘ͕ؔ૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍ ISO8601Ͱͷهड़͕ͪΐͬͱͭΒ͍ (ݸਓతʹ)
Now with Kotlin
Now with Kotlin • KotlinAnnotationʹίϯετϥΫλΛએݴͰ͖Δ • ͜ͷػೳΛͬͯɺISO8061Ͱͳ͘intܕͰΛද͢ https://github.com/YukiMatsumura/Now#kotlin
@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
class FooTest { @Rule @JvmField val rule = Jsr310TimeRule()
@Now(2017, 5, 1, 0, 0, 0) @Test fun test() { } }
ΧϓηϧԽ͕ଛͳΘΕ͍ͯͳ͍ ςετରΫϥεʹςετઐ༻ͷίʔυ͕ೖΒͳ͍ ςετରΫϥεͷґଘ͕ؔ૿͑ͳ͍ ϘΠϥʔϓϨʔτ͕গͳ͍ ࣌ΛIntͰදݱͰ͖ɺannotate͘͢͠ɺಡΈ͍͢(ݸਓతʹ)
·ͱΊ • ݱࡏ࣌ࠁʹґଘͨ͠ςετͷΞϓϩʔνෳ • ͲΕҰҰ • AnnotationΛ༻͍ͨํ๏ັྗతͳΞϓϩʔν ͥͻݕ౼ͯ͠Έ͍ͯͩ͘͞
͋Γ͕ͱ͏͍͟͝·ͨ͠