@Test public void mockFieldWithCapturingLimitedToOneInstance() { new NonStrictExpectations() { { mock2.getStr(); result = "mocked"; } }; assertNull(preexistingInstance.getStr()); BaseClass service1 = new DerivedClass("test 1"); assertNull(service1.str); assertEquals("mocked", service1.getStr()); BaseClass service2 = new BaseClass("test 2"); assertNull(service2.str); assertNull(service2.getStr()); new Verifications() { { mock2.getStr(); times = 1; preexistingInstance.getStr(); times = 1; } }; }
@BeforeClass public static void createAndUseInstancesBeforeCapturingIsInEffect() { assertEquals(1, new ServiceImpl().doSomething()); preexistingInstance = new DerivedClass(); assertEquals("", preexistingInstance.getStr()); assertEquals("test1", new DerivedClass("test1").getStr()); }