Use of Java testing framework jmock

Source: Internet
Author: User

1. Introduction to jmock

2. jmock Principle

2.1 capture expection

2.2 capture invokation

 

3. Expand jmock

package com.amazon.oih.fasturl.config.oihscope;import java.util.*;import oracle.net.aso.p;import org.hamcrest.Description;import org.jmock.api.Action;import org.jmock.api.Invocation;public class JMockExt {            interface Provider{        Object get();    }        public static class ReturnInstanceAction implements Action {                private Provider provider;                public ReturnInstanceAction(Provider p ){            this.provider = p;        }          @Override        public void describeTo(Description description) {            description.appendText(" return a instance created by " + p.class.getSimpleName());        }        @Override        public Object invoke(Invocation invocation) throws Throwable {            return provider.get();        }            }        public static class ReturnInstancesAction implements Action {                private Provider provider;        private int num;                public ReturnInstancesAction (Provider p ,int num){            this.provider = p;            this.num = num;        }          @Override        public void describeTo(Description description) {            description.appendText(" return "+ num +" instances created by " + p.class.getSimpleName());        }        @Override        public Object invoke(Invocation invocation) throws Throwable {            List list = new ArrayList();            int i= 0;            while(i++ < num){                list.add(provider.get());            }            return list;        }            }               public static class Expectations extends org.jmock.Expectations{        public static Action returnInstance(Provider provider) {            return new ReturnInstanceAction(provider);        }             public static Action returnInstances(Provider provider, int num) {            return new ReturnInstancesAction(provider, num);        }         public static Action returnInstances(Provider provider) {            return new ReturnInstancesAction(provider, 1);        }     }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.