Last active
August 29, 2015 14:07
-
-
Save ossan-pg/51c7b0db845242a550ed to your computer and use it in GitHub Desktop.
JMockit を使用して LocalDateTime.now() で指定した日時を取得する.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 本家(?)のページの方がスマートなのでそっちを見よう. | |
* http://jmockit.googlecode.com/svn/trunk/samples/fakingXmocking/test/java8testing/Java8SupportTest.java | |
* mockLocalDateTime() を参照. | |
*/ | |
package hoge; | |
import java.time.Clock; | |
import java.time.LocalDateTime; | |
import mockit.Invocation; | |
import mockit.Mock; | |
import mockit.MockUp; | |
import org.junit.Test; | |
public class HogeTest { | |
/** | |
* LocalDateTime.now() で指定した日時を取得する. | |
*/ | |
@Test | |
public void test() { | |
new MockUp<LocalDateTime>() { | |
@Mock | |
public LocalDateTime now(Invocation inv) { | |
// LocalDateTime.now() は内部で LocalDateTime.now(Clock) を呼び出しているので | |
// 日の値は 15 になる. | |
LocalDateTime now = inv.proceed(); | |
return now.withYear(2000).withMonth(1); // .withDayOfMonth(14); | |
} | |
@Mock | |
public LocalDateTime now(Invocation inv, Clock clock) { | |
LocalDateTime now = inv.proceed(clock); | |
return now.withYear(2001).withMonth(2).withDayOfMonth(15); | |
} | |
}; | |
System.out.println(LocalDateTime.now()); | |
System.out.println(LocalDateTime.now(Clock.systemUTC())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment