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
840
Annotationを利用した現在時刻のテスト
Android Testing Bootcamp #6 で発表した資料です
えぐ
May 10, 2017
Tweet
Share
More Decks by えぐ
See All by えぐ
KotshiからMoshi-codegenに 乗り換えた経緯
egugue
0
2.3k
エンジニアにしてもよいこと エンジニアからお願いしたいこと
egugue
0
170
コードで事前条件を表明する
egugue
3
3.4k
Backends for Frontends を適用した話
egugue
1
1.1k
FlexboxLayoutの紹介
egugue
2
310
Robolectricの @Configを共通化する方法
egugue
0
820
Featured
See All Featured
Scaling GitHub
holman
458
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
A Tale of Four Properties
chriscoyier
157
23k
For a Future-Friendly Web
brad_frost
175
9.4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
We Have a Design System, Now What?
morganepeng
51
7.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Building Applications with DynamoDB
mza
91
6.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Code Reviewing Like a Champion
maltzj
520
39k
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Λ༻͍ͨํ๏ັྗతͳΞϓϩʔν ͥͻݕ౼ͯ͠Έ͍ͯͩ͘͞
͋Γ͕ͱ͏͍͟͝·ͨ͠