清单 10. 场景 4 的测试示例
public class MyNewClassTest extends RMockTestCase{ private MyNewClass myClass; private MockObjectTestCase testCase; private Collaborator collaborator; private Mock mockClassB; public void setUp(){ myClass = new MyNewClass(); testCase = new MyMockObjectTestCase(); mockClassB = testCase.mock(ClassB.class, "mockClassB"); mockClassB.expects(testCase.once()).method("wierdMethod"). will(testCase.returnValue("passed")); Class[] someClassArray = new Class[]{String.class, ClassA.class, ClassB.class}; Object[] someObjectArray = new Object[] {"someArbitraryString", new ClassA(), (ClassB)mockClassB.proxy()}; collaborator = (Collaborator)intercept (Collaborator.class, someClassArray, someObjectArray, "mockCollaborator"); } public void testRMockAndJMockInCollaboration(){ startVerification(); assertTrue(myClass.executeJob(collaborator)); } private class MyMockObjectTestCase extends MockObjectTestCase{} private class MyNewClass{ public boolean executeJob(Collaborator collaborator){ collaborator.executeSomeImportantFunction(); return true; } } } |