Java 原生实现CallBack、JUnit 单元测试简单使用

Java 原生实现CallBack

 

 

定义一个callBack 函数接口:

 

/**
 * 版权所有 (c) 2016,xiaoming有限公司  
 */
package helloTest;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年1月9日    Create this file
 * </pre>
 * 
 */

public interface CallBackClass {
    public void whenCall();
}

 

 

 

类的函数执行完之后进行调用callBack 函数接口:

 

/**
 * 版权所有 (c) 2016,xiaoming有限公司  
 */
package helloTest;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年1月9日    Create this file
 * </pre>
 * 
 */

public class TestCallBack {
    public void work(int n, CallBackClass callback){
        for(int i = 0; i < n; i++){
            System.out.println("the "+ i+" times");
        }
        callback.whenCall();
    }

}

 

 

 

 

 

写一个测试类测试 TestClallBack 执行后是否能够执行 callBack 接口调用

 

/**
 * 版权所有 (c) 2016,xiaoming有限公司  
 */
package helloTest;
/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年1月9日    Create this file
 * </pre>
 * 
 */

public class CallBackTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        new TestCallBack().work(100, new CallBackClass() {
            
            @Override
            public void whenCall() {
                System.out.println("end Run");
            }
        });
    }

}

 

 

add Libraries:

 

2、


编写JUnit 简单测试:

 

package helloTest;

import junit.framework.TestCase;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * DELL          2017年1月9日    Create this file
 * </pre>
 * 
 */

public class CallBackFunctionTest extends TestCase {

    /**用setUp进行初始化操作*/  
    public void setUp() {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void testCallBackFunction() {

        new TestCallBack().work(100, new CallBackClass() {

            @Override
            public void whenCall() {
                System.out.println("end Run");
            }
        });

    }

    public void print(String s) {
        System.out.println(s);
    }
    
    /**用tearDown来销毁所占用的资源*/  
    protected void tearDown() throws Exception {   
        super.tearDown();   
        //System.gc();   
    }   
}


3、run-> jUnitTest:

 

执行结果:

 

the 0 times
the 1 times
the 2 times
the 3 times
the 4 times
the 5 times
the 6 times
the 7 times
the 8 times
the 9 times
the 10 times
the 11 times
the 12 times
the 13 times
the 14 times
the 15 times
the 16 times
the 17 times
the 18 times
the 19 times
the 20 times
the 21 times
the 22 times
the 23 times
the 24 times
the 25 times
the 26 times
the 27 times
the 28 times
the 29 times
the 30 times
the 31 times
the 32 times
the 33 times
the 34 times
the 35 times
the 36 times
the 37 times
the 38 times
the 39 times
the 40 times
the 41 times
the 42 times
the 43 times
the 44 times
the 45 times
the 46 times
the 47 times
the 48 times
the 49 times
the 50 times
the 51 times
the 52 times
the 53 times
the 54 times
the 55 times
the 56 times
the 57 times
the 58 times
the 59 times
the 60 times
the 61 times
the 62 times
the 63 times
the 64 times
the 65 times
the 66 times
the 67 times
the 68 times
the 69 times
the 70 times
the 71 times
the 72 times
the 73 times
the 74 times
the 75 times
the 76 times
the 77 times
the 78 times
the 79 times
the 80 times
the 81 times
the 82 times
the 83 times
the 84 times
the 85 times
the 86 times
the 87 times
the 88 times
the 89 times
the 90 times
the 91 times
the 92 times
the 93 times
the 94 times
the 95 times
the 96 times
the 97 times
the 98 times
the 99 times
end Run

 

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiaoming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值