The JMockit Tutorial より抜粋 http://jmockit.googlecode.com/svn/trunk/www/tutorial/BehaviorBasedTesting.html
@Test public void someTestMethod(@Mocked final DependencyAbc abc) { new NonStrictExpectations() {{ abc.intReturningMethod(anyInt, null); result = new Delegate() { int aDelegateMethod(int i, String s) { return i == 1 ? i : s.length(); } }; }}; new UnitUnderTest().doSomething(); }
@Test public void verifyExpectationWithArgumentValidatorForEachInvocation( @Mocked final Collaborator mock) { // Inside tested code: new Collaborator().doSomething(0.5, new int[2], "test"); new Verifications() {{ mock.doSomething(anyDouble, null, null); forEachInvocation = new Object() { void validate(double d, int[] i, String s) { assertTrue(d > 0.0); assertEquals(2, i.length); assertTrue(s.length() > 1); } }; }}; }