@Test public void verificationInOrderIgnoringStubs( @Mocked final MockedClass mock, @Mocked final MockedClass mockTwo) { // Stubbings, with an invocation count constraint for later (automatic) verification: new Expectations() { { mock.getItem(1); result = "ignored"; times = 1; } }; // In tested code: mock.doSomething("a", true); mockTwo.someMethod("b"); mock.getItem(1); // Verify all invocations, except those verified implicitly through recorded invocation // count constraints. // There is no support for ignoring stubbings that were not verified in any way. // That said, note the API does not require any code duplication. new FullVerificationsInOrder() { { mock.doSomething("a", true); mockTwo.someMethod("b"); } }; }
// Equivalent to "spyingOnRealObjects", but real implementations execute only on replay. @Test // Uses of JMockit API: 7 public void dynamicPartialMocking() { final MockedClass mock = new MockedClass(); // Mocks a real object: new Expectations(mock) {}; // Optionally, you can record some invocations: new Expectations() { { mock.getSomeValue(); result = 100; } }; // When recording invocations on any mocked instance, partially mocked or not, real // implementations // are never executed, so this call would never throw an exception: new Expectations() { { mock.getItem(1); result = "an item"; } }; // Using the mock calls real methods, except for calls which match recorded expectations: mock.doSomething("one", true); mock.doSomething("two", false); assertEquals("one", mock.getItem(0)); assertEquals("an item", mock.getItem(1)); assertEquals(100, mock.getSomeValue()); // Optionally, you can verify invocations to the dynamically mocked types/objects: new Verifications() { { // when verifying invocations, real implementations are never executed mock.doSomething("one", true); mock.doSomething("two", anyBoolean); } }; }
@Test // Uses of JMockit API: 3 public void capturingArgumentForVerification(@Mocked final MockedClass mock) { mock.doSomething(new Person("John")); new Verifications() { { Person argument; mock.doSomething(argument = withCapture()); assertEquals("John", argument.getName()); } }; }
@Test // Uses of JMockit API: 2 public void verifyBehavior(@Mocked final MockedClass mock) { // Mock is used (replay phase): mock.doSomething("one", true); mock.someMethod("test"); // Invocations to mock are verified (verify phase): new Verifications() { { mock.doSomething("one", true); mock.someMethod("test"); } }; }
@Test // Uses of JMockit API: 4 public void capturingMultipleArgumentsForVerification(@Mocked final MockedClass mock) { mock.doSomething("test", true); new Verifications() { { String captor1; boolean captor2; mock.doSomething(captor1 = withCapture(), captor2 = withCapture()); assertEquals("test", captor1); assertTrue(captor2); } }; }
@Test // Uses of JMockit API: 2 public void nonGreedyVerificationInOrder(@Mocked final MockedClass mock) { mock.someMethod("some arg"); mock.someMethod("some arg"); mock.someMethod("some arg"); mock.doSomething("testing", true); mock.someMethod("some arg"); new VerificationsInOrder() { { mock.someMethod("some arg"); minTimes = 2; mock.doSomething("testing", true); } }; }
@Test // Uses of JMockit API: 3 public void capturingArgumentsForVerification(@Mocked final MockedClass mock) { mock.doSomething(new Person("John")); mock.doSomething(new Person("Jane")); new Verifications() { { List<Person> capturedPeople = new ArrayList<>(); mock.doSomething(withCapture(capturedPeople)); times = 2; assertEquals("John", capturedPeople.get(0).getName()); assertEquals("Jane", capturedPeople.get(1).getName()); } }; }