Steve
Steve

Reputation: 4668

Initialising variables in a mock class scala

I am writing unit tests for an akka actor model implementation. The system contains classes and traits that need to be initialised. My issue lies with the testing of the methods. When I mock required parameters for a class, it removes the intelij compiler error, however all of the variables are set to null.

I have attempted to use

         when(mock.answer).thenReturn(42)

and directly assigning the variables

        val mock.answer = 42

The above two through compilation errors. "When" is not recognised and directly assigning values cases a runtime error.

Any insight would be much appreciated.

Upvotes: 0

Views: 2771

Answers (1)

proximator
proximator

Reputation: 688

I am not sure if I understood your issue correctly, but try the self contained code snippet below and let me know if it is not clear enough:

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FunSuite, Matchers}
import org.mockito.Mockito.when

@RunWith(classOf[JUnitRunner])
class MyTest extends FunSuite with Matchers with MockitoSugar {

  trait MyMock {
    def answer: Int
  }

  test("my mock") {
    val myMock = mock[MyMock]
    when(myMock.answer).thenReturn(42)

    myMock.answer should be(42)
  }
}

Upvotes: 4

Related Questions