0
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 3 years have passed since last update.

Scala + Mockitoで入れ子のフィールドをモック

Posted at

下記コードのようにmockメソッドの引数に設定情報を渡してやると、入れ子のフィールドをモックできます。

import org.specs2.mock.Mockito

object Main extends App with Mockito {
  import org.mockito.Answers
  import org.mockito.Mockito.withSettings

  case class Hoge(fuga: Fuga)
  case class Fuga(fuge: String)
  
  val hoge = mock[Hoge](withSettings.defaultAnswer(Answers.RETURNS_DEEP_STUBS))
  hoge.fuga.fuge returns "f u g e"
  println(hoge.fuga.fuge) // f u g e
}

(もし仮に単にval hoge = mock[Hoge]と書いてhogeを定義してやると、Null Pointer Exceptionになります。)

0
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
0
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?