title: Java Easymock complains with "java.lang.IllegalStateException: void method cannot return a value" or "no last call on a mock available"
body: <p>We are using EasyMock for JUnit testing of our Java application inside Eclipse. Using code similar to the below, we found a strange behaviour: when running the full test suite (Eclipse Project -> Run as -> JUnit) one test case fails reproducibly. However when running it standalone it works fine.</p>
  
  <p>Interface:</p>
  
  <pre><code>package de.zefiro.java.easymockexception;
  
  public interface Fruit {
      public String fall();
  }
  </code></pre>
  
  <p>Test class:</p>
  
  <pre><code>package de.zefiro.java.easymockexception;
  
  import static org.easymock.EasyMock.createNiceMock;
  import static org.easymock.EasyMock.expect;
  import static org.easymock.EasyMock.replay;
  import static org.junit.Assert.assertTrue;
  
  import org.junit.BeforeClass;
  import org.junit.Test;
  
  public class Newton {
      private static final Fruit APPLE = createNiceMock(Fruit.class);
  
      @BeforeClass
      public static void SetUpClass() {
          expect(APPLE.fall()).andReturn("Targeting HEAD").anyTimes();
          replay(APPLE);
      }
  
      @Test
      public void testGravity() {
          String target = APPLE.fall();
          assertTrue("Missed", target.contains("HEAD"));
      }
  }
  </code></pre>
  
  <p>Test suite:</p>
  
  <pre><code>package de.zefiro.java.easymockexception;
  
  import org.junit.runner.RunWith;
  import org.junit.runners.Suite;
  import org.junit.runners.Suite.SuiteClasses;
  
  @RunWith(value = Suite.class)
  @SuiteClasses( { Newton.class } )
  public class ScienceTests { }
  </code></pre>
  
  <p>Running all tests on the Eclipse project - i.e. both ScienceTests calling Newton as well as Newton directly - produced this exception in the above small example:</p>
  
  <pre><code>java.lang.IllegalStateException: no last call on a mock available
  at org.easymock.Easymock.getControlForLastCall(EasyMock.java:175)
  </code></pre>
  
  <p>There is a <a href="https://stackoverflow.com/questions/3494969/easymock-3-0-mocking-class-throws-java-lang-illegalstateexception-no-last-call">similar question here</a>, but it seems to be unrelated.</p>
  
  <p>And in our real testing code (bigger class, but the main actors are identical to the stripped-down example) this exception:</p>
  
  <pre><code>java.lang.IllegalStateException: void method cannot return a value
  at org.easymock.internal.MocksControl.andReturn(MocksControl.java:101)
  </code></pre>
  
  <p>I didn't find an answer either on Google nor here on StackOverflow, but found out myself now, so in the spirit of <a href="https://meta.stackexchange.com/questions/2706/posting-and-answering-questions-you-have-already-found-the-answer-to" title="Authoritive Answer">answering your own questions</a> I'll post my findings below. Worth mentioning is also this post I found, even though it didn't help me in this particular case: <a href="http://rockycode.com/blog/easymock-cause-effect-exception-mapping/" rel="nofollow noreferrer">EasyMock Cause-Effect Exception Mapping</a></p>