mercredi 28 octobre 2015

Mockito class is mocked but returns nothing

I am kinda new with Scala and as said in the title, i am trying to mock a class.

DateServiceTest.scala

@RunWith(classOf[JUnitRunner])
class DateServiceTest extends FunSuite with MockitoSugar {
  val conf = new SparkConf().setAppName("Simple Application").setMaster("local")
  val sc = new SparkContext(conf)
  implicit val sqlc = new SQLContext(sc)  

  val m = mock[ConfigManager]
  when(m.getParameter("dates.traitement")).thenReturn("10")

  test("mocking test") {

    val instance = new DateService
    val date = instance.loadDates
    assert(date === new DateTime())
  }
}      

DateService.scala

class DateService extends Serializable with Logging {

  private val configManager = new ConfigManager
  private lazy val datesTraitement = configManager.getParameter("dates.traitement").toInt

  def loadDates() {
    val date = selectFromDatabase(datesTraitement)
  }
}

Unfortunately when I run the test, datesTraitement returns null instead of 10, but m.getparameter("dates.traitement") does return 10.

Maybe i am doing some kind of anti pattern somewhere but I don't know where, please keep in mind that I am new with all of this and I didn't find any proper example specific to my case on internet.

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire