@Test // Uses of JMockit API: 8
  public void useArgumentMatchers() {
    new Expectations() {
      {
        // Using built-in matchers:
        mockedList.get(anyInt);
        result = "element";

        // Using Hamcrest matchers:
        mockedList.get(withArgThat(is(equalTo(5))));
        result = new IllegalArgumentException();
        minTimes = 0;
        mockedList.contains(withArgThat(hasProperty("bytes")));
        result = true;
        mockedList.containsAll(withArgThat(hasSize(2)));
        result = true;
      }
    };

    assertEquals("element", mockedList.get(999));
    assertTrue(mockedList.contains("abc"));
    assertTrue(mockedList.containsAll(asList("a", "b")));

    new Verifications() {
      {
        mockedList.get(anyInt);
      }
    };
  }
  @Test(expected = UnexpectedInvocation.class) // Uses of JMockit API: 2
  public void verifyThatInvocationsNeverHappenedWhenTheyDid(@Mocked List<String> mockTwo) {
    mockedList.add("one");
    mockTwo.size();

    new FullVerifications() {
      {
        mockedList.add("one");
      }
    };
  }
  @Test(expected = UnexpectedInvocation.class) // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrderWhenMoreOfThemHappen() {
    mockedList.add("one");
    mockedList.add("two");
    mockedList.size();

    new FullVerificationsInOrder() {
      {
        mockedList.add("one");
        mockedList.add("two");
      }
    };
  }
  @Test // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrder() {
    mockedList.add("one");
    mockedList.size();
    mockedList.add("two");

    new FullVerificationsInOrder() {
      {
        mockedList.add("one");
        mockedList.size();
        mockedList.add("two");
      }
    };
  }
  @Test // Uses of JMockit API: 3
  public void verifyInOrder(
      @Mocked final List<String> firstMock, @Mocked final List<String> secondMock) {
    // Using mocks:
    firstMock.add("was called first");
    secondMock.add("was called second");

    new VerificationsInOrder() {
      {
        // Verifies that firstMock was called before secondMock:
        firstMock.add("was called first");
        secondMock.add("was called second");
      }
    };
  }
  @Test // Uses of JMockit API: 3
  public void returningElementsFromAList() {
    final List<String> list = asList("a", "b", "c");

    new Expectations() {
      {
        mockedList.get(anyInt);
        result = list;
      }
    };

    assertEquals("a", mockedList.get(0));
    assertEquals("b", mockedList.get(1));
    assertEquals("c", mockedList.get(2));
    assertEquals("c", mockedList.get(3));
  }
  @Test(expected = RuntimeException.class) // Uses of JMockit API: 2
  public void stubVoidMethodsWithExceptions() {
    new Expectations() {
      {
        // void/non-void methods are handled the same way, with a consistent API:
        mockedList.clear();
        result = new RuntimeException();
      }
    };

    mockedList.clear();
  }
  @Test(expected = MissingInvocation.class) // Uses of JMockit API: 1
  public void verifyAllInvocationsInOrderWithOutOfOrderVerifications() {
    mockedList.add("one");
    mockedList.add("two");

    new FullVerificationsInOrder() {
      {
        mockedList.add("two");
        mockedList.add("one");
      }
    };
  }
  @Test // Uses of JMockit API: 8
  public void verifyNumberOfInvocations() {
    // Using mock:
    mockedList.add("once");

    mockedList.add("twice");
    mockedList.add("twice");

    mockedList.add("three times");
    mockedList.add("three times");
    mockedList.add("three times");

    new Verifications() {
      {
        // Following two verifications work exactly the same:
        mockedList.add("once"); // minTimes == 1 is the default
        mockedList.add("once");
        times = 1;

        // Verifies exact number of invocations:
        mockedList.add("twice");
        times = 2;
        mockedList.add("three times");
        times = 3;

        // Verifies no invocations occurred:
        mockedList.add("never happened");
        times = 0;

        // Verifies min/max number of invocations:
        mockedList.add("three times");
        minTimes = 1;
        mockedList.add("three times");
        minTimes = 2;
        mockedList.add("three times");
        maxTimes = 5;
      }
    };
  }
  @Test // Uses of JMockit API: 3
  public void stubAndVerifyInvocationWithoutRepeatingItInExpectationAndVerificationBlocks() {
    new Expectations() {
      {
        // Notice that this can't be done in Mockito, which requires the repetition of
        // "mockedList.get(0);" in the verification phase.
        mockedList.get(0);
        result = "first";
        times = 1;
      }
    };

    assertEquals("first", mockedList.get(0));
  }
  @Test // Uses of JMockit API: 1
  public void verifyAllInvocations() {
    mockedList.add("one");
    mockedList.add("two");

    // Verify all invocations to mockedList.
    new FullVerifications() {
      {
        // Verifies first invocation:
        mockedList.add("one");

        // Verifies second (and last) invocation:
        mockedList.add("two");
      }
    };
  }
  @Test // Uses of Mockito API: 5
  public void returningLastArgument() {
    new Expectations() {
      {
        mockedList.set(anyInt, anyString);
        result =
            new Delegate() {
              String lastArg(int i, String s) {
                return s;
              }
            };
      }
    };

    assertEquals("test", mockedList.set(1, "test"));
  }
  @Test // Uses of JMockit API: 3
  public void customArgumentMatcherUsingAnonymousClass() {
    mockedList.addAll(asList("one", "two"));

    new Verifications() {
      {
        mockedList.addAll(
            with(
                new Delegate<List<String>>() {
                  boolean matches(List<?> list) {
                    return list.size() == 2;
                  }
                }));
      }
    };
  }
  @Test // Uses of JMockit API: 3
  public void stubAndVerifyInvocation() {
    // A recorded expectation is expected to occur at least once, by default.
    new Expectations() {
      {
        mockedList.get(0);
        result = "first";
      }
    };

    assertEquals("first", mockedList.get(0));

    // Note that verifying a stubbed invocation isn't "just redundant" if the test cares that the
    // invocation occurs at least once. If this is the case, then it's not safe to expect the test
    // to break without an explicit verification, because the method under test may never call the
    // stubbed one, and that would be a bug that the test should detect.
  }
  @Test // Uses of JMockit API: 2
  public void stubbingVoidMethods() {
    // The API is consistent, so this is the same as for non-void methods:
    new Expectations() {
      {
        mockedList.clear();
        result = new RuntimeException();
      }
    };

    try {
      // Following throws RuntimeException:
      mockedList.clear();
      fail();
    } catch (RuntimeException ignore) {
    }
  }
  @Test // Uses of JMockit API: 4
  public void verifyThatInvocationsNeverHappened(
      @Mocked List<String> mockTwo, @Mocked List<String> mockThree) {
    // Using mocks - only mockedList is invoked:
    mockedList.add("one");

    // Verify that the two other mocks were never invoked.
    new FullVerifications() {
      {
        // Ordinary verification:
        mockedList.add("one");

        // Verify that method was never called on a mock:
        mockedList.add("two");
        times = 0;
      }
    };
  }
  @Test // Uses of JMockit API: 5
  public void customArgumentMatcherUsingNamedClass() {
    class IsListOfTwoElements implements Delegate<List<String>> {
      boolean matches(List<?> list) {
        return list.size() == 2;
      }
    }

    new Expectations() {
      {
        mockedList.addAll(with(new IsListOfTwoElements()));
        result = true;
        times = 1;
      }
    };

    mockedList.addAll(asList("one", "two"));

    // No need to re-verify the invocation of "addAll" here.
  }
 boolean matches(List<?> list) {
   return list.size() == 2;
 }