title: EasyMock capture: use getValue() outside of an IAnswer
body: <p>Suppose I want to test a method <code>Foo.doSomething(String arg):String</code> using <strong>EasyMock</strong>. I know that I can use <em>EasyMock Capture</em> in order to capture the value set inside of the parameter and then get it at a later time. When writing my test, my goal is to capture the value when I mock out Foo's method and then get the value of the capture outside of my mocked method. I am trying something like this:</p>
  
  <pre><code>Capture&lt;String&gt; stringCapture = new Capture&lt;String&gt;();
  EasyMock.expect(foo.doSomething(EasyMock.capture(stringCapture)).andAnswer(new IAnswer&lt;String&gt; {
      @Override
      public String answer() throws Throwable {
          ...
  });
  String retrievedValue = stringCapture.getValue();
  </code></pre>
  
  <p>However, I get a runtime error when I try to use <code>getValue()</code>:</p>
  
  <pre><code>java.lang.AssertionError: Nothing captured yet
      at org.easymock.Capture.getValue(Capture.java:80)
      at com.example.Test.myTest(...)
      ....
  </code></pre>
  
  <p>I wish I could have a <code>String</code> variable within my test method but outside of the <code>IAsnwer</code> closure and set that variable's value inside of my <code>IAnswer.answer()</code> method. The problem is that the <code>String</code> variable must be marked as <code>final</code> in order to be accessible to the closure, which makes it unmodifiable and hence useless. Also, I do not wish to use a global just to solve this.</p>
  
  <p>The only workaround I've thought of it to use my own class that has a String field, declare it as a <code>final</code> variable outside of <code>IAnswer</code>, set the captured value during <code>answer()</code> and finally set this value into a field within my test method. Perhaps there's a better way to do this because using another class to extract a captured value, or even a global variable, seems ugly to me.</p>