@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 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: 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: 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")); }