title: Test a nested method call on a mocked class using ScalaMock
body: <p>I am new to both ScalaMock and mocking in general. I am trying to test a method which calls a method in another (mocked) class and then calls a method on the returned object.</p>
  
  <p><strong>Detailed information:</strong></p>
  
  <p>So I am using ScalaTest and there are five classes involved in this test...</p>
  
  <p>SubInstruction which I am testing</p>
  
  <pre><code>class SubInstruction(label: String, val result: Int, val op1: Int, val op2: Int) extends Instruction(label, "sub") {
  
    override def execute(m: Machine) {
      val value1 = m.regs(op1)
      val value2 = m.regs(op2)
      m.regs(result) = value1 - value2
    }
  }
  
  object SubInstruction {
    def apply(label: String, result: Int, op1: Int, op2: Int) =
      new SubInstruction(label, result, op1, op2)
  }
  </code></pre>
  
  <p>Machine which must be mocked for the test</p>
  
  <pre><code>case class Machine(labels: Labels, prog: Vector[Instruction]) {
    private final val NUMBEROFREGISTERS = 32
    val regs: Registers = new Registers(NUMBEROFREGISTERS)
  
    override def toString(): String = {
      prog.foldLeft("")(_ + _)
    }
  
    def execute(start: Int) =
      start.until(prog.length).foreach(x =&gt; prog(x) execute this)
  }
  
  object Machine extends App {
    if (args.length == 0) {
      println("Machine: args should be sml code file to execute")
    } else {
      println("SML interpreter - Scala version")
  
      val m = Translator(args(0)).readAndTranslate(new Machine(Labels(), Vector()))
  
      println("Here is the program; it has " + m.prog.size + " instructions.")
      println(m)
      println("Beginning program execution.")
      m.execute(0)
      println("Ending program execution.")
      println("Values of registers at program termination:")
      println(m.regs + ".")
    }
  }
  </code></pre>
  
  <p>Registers which is required to construct a Machine object</p>
  
  <pre><code>case class Registers(size: Int) {
    val registers: Array[Int] = new Array(size)
  
    override def toString(): String =
      registers.mkString(" ")
  
    def update(k: Int, v: Int) = registers(k) = v
    def apply(k: Int) = registers(k)
  }
  </code></pre>
  
  <p>MockableMachine which I have created as the original Machine class does not have an empty constructor and therefore (as I understand) can not be mocked</p>
  
  <pre><code>class MockableMachine extends Machine(Labels(), Vector()){
  
  }
  </code></pre>
  
  <p>and finally my test class SubInstructionTest which compiles but throws the exception below.</p>
  
  <pre><code>class SubInstructionTest extends FlatSpec with MockFactory with Matchers {
  
    val label1 = "f0"
    val result1 = 25
    val op1_1 = 24
    val op2_1 = 20
    val sub1 = SubInstruction(label1, result1, op1_1, op2_1)
  
    "A SubInstruction" should "retrieve the operands from the correct registers in the given machine " +
      "when execute(m: Machine) is called, and perform the operation saving the " +
      "result in the correct register." in {
      val mockMachine = mock[MockableMachine]
  
      inSequence {
        (mockMachine.regs.apply _).expects(op1_1).returning(50)
        (mockMachine.regs.apply _).expects(op2_1).returning(16)
        (mockMachine.regs.update _).expects(result1, 34)
      }
      sub1.execute(mockMachine)
  
    }
  
  }
  </code></pre>
  
  <p><strong>Throws:</strong></p>
  
  <p><strong>java.lang.NoSuchMethodException: Registers.mock$apply$0()</strong></p>
  
  <p>-</p>
  
  <p>I have been searching for a straightforward way to mock this class for hours, but have found nothing. For the time being I have settled on the workaround detailed below, but I was under the impression that mocking would offer a less convoluted solution to the problem of testing my SubInstruction class. </p>
  
  <p><strong>The workaround:</strong></p>
  
  <p>Delete the MockableMachine class and create a CustomMachine class which extends Machine and replaces the registers value with mockedRegisters provided at construction time. </p>
  
  <pre><code>class CustomMachine (mockedRegister: Registers) extends Machine(Labels(), Vector()) {
    override
    val regs: Registers = mockedRegister
  }
  </code></pre>
  
  <p>a MockableRegisters class which I have created as the original does not have an empty constructor and therefore (as I understand) can not be mocked</p>
  
  <pre><code>class MockableRegisters extends Registers(32) {
  
  }
  </code></pre>
  
  <p>and the SubInstructionTest class written in a slightly different way </p>
  
  <pre><code>class SubInstructionTest extends FlatSpec with MockFactory with Matchers {
  
    val label1 = "f0"
    val result1 = 25
    val op1_1 = 24
    val op2_1 = 20
    val sub1 = SubInstruction(label1, result1, op1_1, op2_1)
  
    "A SubInstruction" should "retrieve the operands from the correct registers in the given machine " +
      "when execute(m: Machine) is called, and perform the operation saving the " +
      "result in the correct register." in {
      val mockRegisters = mock[MockableRegisters]
      val machine = new CustomMachine(mockRegisters)
  
      inSequence {
        (mockRegisters.apply _).expects(op1_1).returning(50)
        (mockRegisters.apply _).expects(op2_1).returning(16)
        (mockRegisters.update _).expects(result1, 34)
      }
      sub1.execute(machine)
  
    }
  
  }
  </code></pre>
  
  <p><strong>As indicated, this feels like a workaround to me, is there not a simpler way to do this (perhaps similar to my original attempt)?</strong></p>
  
  <p>I have just included the essential code to ask the question, but you can find the full code on my <a href="https://github.com/BBK-SDP-2015-ahormo01/individual" rel="nofollow">GitHub account</a>. </p>