title: Scala: replace method at runtime
body: <p>Let's say I have a class</p>
  
  <pre><code>class Original {
    def originalMethod = 1
  }
  </code></pre>
  
  <p>Now, let's say I have an instance of it</p>
  
  <pre><code>val instance = new Original
  </code></pre>
  
  <p>Is it now possible to do something to <code>instance</code> at runtime to replace the <code>originalMethod</code> with a different method? (granted the signature stays the same)</p>
  
  <p>For example, now, when calling <code>instance.originalMethod</code> the following code will be called <code>println("test"); 1</code></p>
  
  <p><strong>EDIT</strong>
  I can't call <code>new Original</code>. I just have an existing instance of it, which I have to modify.</p>
  
  <p><strong>EDIT 2</strong>
  (@Aleksey Izmailov answer) This is a nice solution, but isn't exactly what I'm looking for. I'm thinking more in terms of testing - writing a class "normally", without specifying functions as variables rather than methods </p>
  
  <p>PS. I stumbled on this question when trying to mimic a Mockito spy</p>