@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"); } }; }
@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 stubbingWithCallbacksUsingMockUp() { final MockedClass mock = new MockedClass(); new MockUp<MockedClass>() { @Mock String someMethod(Invocation inv, String s) { assertSame(mock, inv.getInvokedInstance()); return "called with arguments: " + s; } }; assertEquals("called with arguments: foo", mock.someMethod("foo")); }
@Test // Uses of JMockit API: 3 public void stubbingConsecutiveCallsToReturnASequenceOfValues(@Mocked final MockedClass mock) { new Expectations() { { mock.someMethod("some arg"); returns("one", "two", "three"); } }; assertEquals("one", mock.someMethod("some arg")); assertEquals("two", mock.someMethod("some arg")); assertEquals("three", mock.someMethod("some arg")); assertEquals("three", mock.someMethod("some arg")); }
@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: 4 public void stubInvocations(@Mocked final MockedClass mock) { new Expectations() { { mock.getItem(0); result = "first"; mock.getItem(1); result = new RuntimeException(); } }; assertEquals("first", mock.getItem(0)); try { mock.getItem(1); } catch (RuntimeException ignore) { // OK } assertNull(mock.getItem(999)); }
@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: 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: 5 public void returningFirstArgument(@Mocked final MockedClass mock) { new Expectations() { { mock.someMethod(anyString); result = new Delegate() { String firstArg(String s) { return s; } }; } }; assertEquals("test", mock.someMethod("test")); }
@Test // Uses of JMockit API: 5 public void stubbingWithCallbacksUsingDelegate(@Mocked final MockedClass mock) { new Expectations() { { mock.someMethod(anyString); result = new Delegate() { String delegate(String s) { return "called with arguments: " + s; } }; } }; assertEquals("called with arguments: foo", mock.someMethod("foo")); }
@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()); } }; }
@Test // Uses of JMockit API: 7 public void callingRealMethodFromDelegate(@Injectable final MockedClass mock) { new Expectations() { { mock.someMethod(anyString); result = new Delegate() { String delegate(Invocation invocation, String s) { String actualResult = invocation.proceed(); return "Res=" + actualResult; } }; } }; assertEquals("Res=3", mock.someMethod("3")); }
@Test // Uses of JMockit API: 4 public void chainingMethodCallsWithCascading(@Mocked final MockedClass mock) { new Expectations() { { mock.getPerson().getName(); result = "deep"; } }; assertEquals("deep", mock.getPerson().getName()); new Verifications() { { // Not likely to be useful often, but such verifications do work: mock.getPerson(); mock.getPerson().getName(); } }; }
// 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); } }; }