title: JMockit unable to mock more than one instance of class
body: <p>I want to create a Map that holds Strings as its keys and mocked instances of class <code>Candidate</code> as its values.</p>
  
  <pre><code>    Map&lt;String, Long&gt; domainNameToId = new HashMap&lt;String, Long&gt;();
      domainNameToId.put("farmaciapuentezurita.es", 1234l);
      domainNameToId.put("vivefarma.com", 2345l);
      domainNameToId.put("eurofarmacia.com", 3456l);
  
      Map&lt;String, Candidate&gt; expectedCandidates = new HashMap&lt;String, Candidate&gt;();
      for(String domain : domainNameToId.keySet()) {
          final Candidate cand = new MockUp&lt;Candidate&gt;() {
              @Mock Long getDomainId() { return domainNameToId.get(domain); } // private method
              @Mock boolean validateAndPrepare() { return true; }
              @Mock String getRepresentingName() { return domain; }
          }.getMockInstance();
          expectedCandidates.put(domain, cand);
      }
  </code></pre>
  
  <p><strong>The above code used to work before upgrading JMockit from 1.20 to 1.28</strong>.</p>
  
  <p>Now I get an exception:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Invalid attempt to get uninitialized instance of class com.urlservice.data.Candidate from stateless mockup
        at ...</p>
  </blockquote>
  
  <p>I read the documentation and tried to use <code>new MockUp(T targetInstance)</code> instead in the following fashion (this is the loop's body):</p>
  
  <pre><code>final Candidate cand = new Candidate(domain);
  new MockUp&lt;Candidate&gt;(cand) {
      @Mock Long getDomainId() { return domainNameToId.get(domain); }  // private method
      @Mock boolean validateAndPrepare() { return true; }
      @Mock String getRepresentingName() { return domain; }
  };
  </code></pre>
  
  <p>The result was very strange - the first Candidate was mocked properly while the rest of the mocked candidates weren't mocked at all and their real methods were called.</p>
  
  <p>I tried to revert back to the <code>Expectations</code> API:</p>
  
  <pre><code>final Candidate cand = new Candidate(domain);
  new Expectations(cand) {{
      cand.getDomainId(); result = domainNameToId.get(domain);  // Had to make it public :-(
      cand.validateAndPrepare(); result = true;
      cand.getRepresentingName(); result = domain;
  }};
  </code></pre>
  
  <p>to no avail:</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: Already mocked: class com.urlservice.data.Candidate
        at...</p>
  </blockquote>
  
  <p>I really want to upgrade to the latest version but I can't find a workaround to this problem.</p>
  
  <p><strong>UPDATE:</strong> I didn't manage to reproduce this issue in any version up to 1.28, so I guess this is the version in which it was introduced.</p>
  
  <p>In addition, related to my 2nd example (<code>new MockUp(T targetInstance)</code>), I looked at the source code of class <code>MockUp</code> line 402 and it looks to me like the expected behavior is to NOT mock any specific target instance other than the first one:</p>
  
  <pre><code>  MockUp&lt;?&gt; previousMockUp = findPreviouslyFakedClassIfMockUpAlreadyApplied();
  
    if (previousMockUp != null) {
       targetType = previousMockUp.targetType;
       mockedClass = previousMockUp.mockedClass;
       return;  // Input param targetInstance is disregarded
    }
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p><strong>UPDATE2:</strong> I came up with a failing test example. It's a bit cumbersome but I'm sure it will get the point through.</p>
  
  <pre><code>public class SampleTest {
  
  class TestedClass {
      private IncrementingDependency dep;
      TestedClass(IncrementingDependency dep) { this.dep = dep; }
      public int getVal() { return dep.inc(); }
  }
  
  class IncrementingDependency {
      int val;
      public IncrementingDependency(int val) { this.val = val; }
      public int inc() { return ++val; }
  }
  
  @Test
  public void sampleTest() {
      List&lt;Integer&gt; inputVals = Arrays.asList(1, 2, 3);
      List&lt;TestedClass&gt; incrementingClasses = new ArrayList&lt;TestedClass&gt;();
  
      for (Integer num : inputVals) {
          IncrementingDependency dep = new IncrementingDependency(num);
          new MockUp&lt;IncrementingDependency&gt;(dep) {
              @Mock int inc() { return num; }  // Mock with different behavior - DON'T INCREMENT
          };
          incrementingClasses.add(new TestedClass(dep));
      }
  
      assertThat(incrementingClasses.get(0).getVal()).isEqualTo(1); // Passes - 1 wasn't incremented (mocked behavior)
      assertThat(incrementingClasses.get(1).getVal()).isEqualTo(2); // Fails - real code was called and 2 was incremented to 3
      assertThat(incrementingClasses.get(2).getVal()).isEqualTo(3); // We never get to this point
  }
  }
  </code></pre>
  
  <p>Please note that even if this example wouldn't have failed, the fact that I need to instantiate my dependency before passing it to <code>MockUp</code>'s constructor is problematic at best. Isn't the whole point of creating a mock is that you don't need to instantiate it?</p>