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

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

えぐ
May 10, 2017
830

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

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

えぐ

May 10, 2017
Tweet

Transcript

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

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

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

    = System.currentTimeMillis();
 }
 
 @VisibleForTesting String getRelativeTime(Model model, long now) {
 }
 }
  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();
 }
 }
  5. class Foo {
 
 String getRelativeTime(Model model) {
 long now

    = now();
 }
 
 // ςετ࣌ʹpartial mockԽ
 @VisibleForTesting long now() {
 return System.currentTimeMillis();
 }
 }
  6. public class Jsr310Time {
 
 private static Clock clock =

    Clock.systemUTC();
 
 public static long now() {
 return clock.millis();
 }
 
 // ...
 }
  7. public class FooTest {
 
 @Rule Jsr310TimeRule rule = new

    Jsr310TimeRule();
 
 @Now("2017-05-01T00:00:00Z")
 @Test public void test() {
 
 }
 }
  8. @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
  9. class FooTest {
 
 @Rule @JvmField val rule = Jsr310TimeRule()


    
 @Now(2017, 5, 1, 0, 0, 0)
 @Test
 fun test() {
 
 }
 }