title: Mockito fails with "Argument(s) are different" but debugger shows differently
body: <p>I am testing a method, which uses Mockito mocked objects. When I run the test, Mockito tells me that the parameters passed into the argument are different than expected. If I set a breakpoint within the method I am testing, the argument that it is called with is actually what I expect. This method only gets called once. Why is Mockito reporting something different from what I see with the debugger, and how can I fix this?</p>
  
  <p>Here is the test:</p>
  
  <pre><code>@Test
  public void addExclusivePermutationsTest() {
      Permutation p1 = mock(Permutation.class);
  
      PermutationBuilder pb = new PermutationBuilder();
  
      when(p1.applyPermutations(anySetOf(String.class)))
              .thenReturn(Collections.singleton("abc123"));
  
      pb.addExclusivePermutations(p1);
      pb.permute("test");
  
      verify(p1).applyPermutations(new HashSet&lt;&gt;(
              Collections.singletonList("test")));
  }
  </code></pre>
  
  <p>The test is pretty simple. <code>Permutation</code> is an interface that contains the method definition <code>Set&lt;String&gt; applyPermutations(Set&lt;String&gt;)</code>. When that method is called on <code>p1</code>, I tell Mockito to return a set containing <code>[abc123]</code>.</p>
  
  <p>The <code>addExclusivePermutations(p1)</code> call simply adds <code>p1</code> to an <code>ExclusivePermutation</code> object that contains a list of <code>Permutation</code> objects. <code>ExclusivePermutation</code> is an implementor of <code>Permutation</code> so its <code>applyPermutations</code> method looks like this:</p>
  
  <pre><code>public Set&lt;String&gt; applyPermutations(final Set&lt;String&gt; stringPermutations) {
      Set&lt;String&gt; exclusivePermutations = new HashSet&lt;&gt;();
  
      for (Permutation permutation : permutationList) {
          exclusivePermutations.addAll(permutation.applyPermutations(stringPermutations));
      }
  
      return exclusivePermutations;
  }
  </code></pre>
  
  <p>The above method is called inside <code>permute("test")</code>. The string <code>"test"</code> is converted into a <code>Set</code>, and passed into the above method. The above method is what calls <code>applyPermutations</code> on <code>p1</code>. When I look at this method with a debugger, <code>stringPermutations</code> contains only <code>[test]</code>, as I expect it to, and is passed directly into <code>p1</code>. Then <code>p1</code> returns <code>[abc123]</code> because it's mocked, and the above method joins it with <code>stringPermutations</code> to return <code>[test, abc123]</code>. So for some reason, Mockito is saying that <code>stringPermutations</code> is not <code>[test]</code> as the debugger shows, but instead what the method returns.</p>
  
  <p>Finally, here is the error:</p>
  
  <pre><code>Argument(s) are different! Wanted:
  permutation.applyPermutations(
      [test]
  );
  -&gt; at PermutationBuilderTest$AddExclusivePermutationTests.addExclusivePermutationsTest(PermutationBuilderTest.java:87)
  Actual invocation has different arguments:
  permutation.applyPermutations(
      [abc123, test]
  );
  </code></pre>
  
  <p>One last note (for those of you who are still reading). If I bypass <code>ExclusivePermuation</code> by copying <code>ExclusivePermutation.applyPermutation()</code> and putting it directly inside <code>PermutationBuilder</code>, the test passes. It is a mystery...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So I've narrowed it down a bit. In <code>PermutationBuilder</code> I have this method:</p>
  
  <pre><code>public Set&lt;String&gt; permute(String s) {
      for (Modifier modifier : modifierList) {
          s = modifier.applyModification(s);
      }
  
      Set&lt;String&gt; stringPermutations = new HashSet&lt;&gt;(Collections.singletonList(s));
  
      for (Permutation permutation : permutationList) {
          Set&lt;String&gt; result = permutation.applyPermutations(stringPermutations);
          stringPermutations.addAll(result);
      }
  
      return stringPermutations;
  }
  </code></pre>
  
  <p>After I assign result, Mockito says that the method was called with <code>[test]</code>. However, when I step to the next line to add it to <code>stringPermutations</code>, it changes to <code>[abc123, test]</code>. You can see this in the two screenshots below (look at the bottom at the <code>arguments</code> array):</p>
  
  <p><a href="https://i.stack.imgur.com/OacoH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OacoH.png" alt="After getting result"></a></p>
  
  <p><a href="https://i.stack.imgur.com/G1Tji.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G1Tji.png" alt="enter image description here"></a></p>