Search code examples
scalascalatesteasymock

java.lang.IllegalStateException: missing behavior definition for the preceding method call


I have the following test code using Scala 2.11.x with Scalatest and EasyMock (also using EasyMockSugar):

import org.scalatest._
import org.easymock.EasyMock._
import org.scalatest.easymock._

// definition of Grid
trait Grid {
    def steps: Int
}

class MyTestSuite extends FunSuite with Matchers with EasyMockSugar {
  test("First differential correctness") {
    val grid: Grid = mock[Grid]
    val steps = 4
    expect(grid.steps).andReturn(steps)
    // use the grid mock ...
  }
}

However, at Runtime I get the following exception:

java.lang.IllegalStateException: missing behavior definition for the preceding method call:
Grid.steps()

Solution

  • You need to invoke replay on the mock:

    class MyTestSuite extends FunSuite with Matchers with EasyMockSugar {
      test("First differential correctness") {
        val grid: Grid = mock[Grid]
        val steps = 4
        expect(grid.steps).andReturn(steps)
    
        replay(grid)
        // use the grid mock ...
        verify(grid)
      }
    }