jmock測試的準備工作

          private Mockery context = new JUnit4Mockery() {
                  {
                      setImposteriser(ClassImposteriser.INSTANCE);
                  }
              };

          注意這里的setImposteriser(ClassImposteriser.INSTANCE);
          因為默認的JUnit4Mockery的Imposteriser是JavaReflectionImposteriser,他是針對接口進行mock的,如果要針對class進行mock,必須setImposteriser(ClassImposteriser.INSTANCE);
          這樣之后context便可以對class進行mock了


          官方的文檔:

          Mocking Classes with jMock and the ClassImposteriser

          Because it uses Java's standard reflection capability, the default configuration of the jMock framework can only mock interfaces, not classes. (Actually, we consider that to be a good thing because it encourages the design to focus on communication between objects rather than static classification or data storage). However, the ClassImposteriser extension class uses the CGLIB 2.11 and Objenesis2 libraries to create mock objects of classes as well as interfaces. This is useful when working with legacy code to tease apart dependencies between tightly coupled classes.

          To use the ClassImposteriser:

          1. Add jmock-legacy-2.1.0-RCn.jar, cglib-nodep-2.1_3.jar and objenesis-1.0.jar to your CLASSPATH.
          2. Plug the ClassImposteriser into the Mockery of your test class:

            Raw

            import org.jmock.Mockery;
            import org.jmock.Expectations;
            import org.jmock.lib.legacy.ClassImposteriser;

            public class ConcreteClassTest extends TestCase {
            private Mockery context = new Mockery() {{
            setImposteriser(ClassImposteriser.INSTANCE);
            }};

            ...
            }

            JUnit 3

            import org.jmock.Expectations;
            import org.jmock.integration.junit3.MockObjectTestCase;
            import org.jmock.lib.legacy.ClassImposteriser;

            public class ConcreteClassTest extends MockObjectTestCase {
            {
            setImposteriser(ClassImposteriser.INSTANCE);
            }

            ...
            }

            JUnit 4

            import org.jmock.Mockery;
            import org.jmock.Expectations;
            import org.jmock.integration.junit4.JUnit4Mockery;
            import org.jmock.lib.legacy.ClassImposteriser;

            @RunWith(JMock.class)
            public class ConcreteClassTest {
            private Mockery context = new JUnit4Mockery() {{
            setImposteriser(ClassImposteriser.INSTANCE);
            }};

            ...
            }
          3. Your tests can now create mocks of abstract or even concrete classes:
            Graphics g = context.mock(java.awt.Graphics.class);

          posted on 2007-12-05 10:54 劉錚 閱讀(905) 評論(0)  編輯  收藏 所屬分類: Jmock

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 原阳县| 文水县| 漳浦县| 当涂县| 聂拉木县| 兴和县| 宁阳县| 米林县| 巫山县| 托里县| 黄陵县| 象州县| 胶南市| 陈巴尔虎旗| 拉萨市| 德惠市| 台北市| 舞钢市| 五原县| 沁阳市| 门头沟区| 高要市| 西乌珠穆沁旗| 尖扎县| 丹东市| 南安市| 广东省| 昭觉县| 乌鲁木齐县| 苍山县| 桐柏县| 苍梧县| 洪洞县| 原平市| 太仓市| 冀州市| 梁河县| 宜黄县| 三明市| 运城市| 宣汉县|