1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Kotlin*JUNIT】LocalDateTime.now( )をモック化

Posted at

本記事の内容

JUNITでLocalDateTimeクラスをモック化したい場合の記載方法を以下に記します。
※LocalDateクラスも同様の記載でモック可能!!

class HogeServiceTest {
    companion object {
        /** LocalDateTime.now()で返す値 */
        private val mockNowDateTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0, 0)

        @BeforeAll
        @JvmStatic // static関数であることを明示
        fun beforeClass() {
            // LocalDateTime.nowをモック化
            mockkStatic(LocalDateTime::class)
            every { LocalDateTime.now() } returns mockNowDateTime
        }
    }

    // ~以下省略~
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?