少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
第一部分:JMOCK测试接口

package com.abin.lee.mock.jmock;

/**
 * Created with IntelliJ IDEA.
 * User: abin
 * Date: 13-5-6
 * Time: 下午12:23
 * To change this template use File | Settings | File Templates.
 */
public interface UserService {
    public String getMessage(String message);
}





package com.abin.lee.mock.jmock;

/**
 * Created with IntelliJ IDEA.
 * User: abin
 * Date: 13-5-6
 * Time: 下午12:24
 * To change this template use File | Settings | File Templates.
 */
public class UserServiceImpl implements UserService {
    @Override
    public String getMessage(String message) {
        String result="";
        result="hello "+message;
        return result;
    }
}





package com.abin.lee.mock.jmock;

import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Created with IntelliJ IDEA.
 * User: abin
 * Date: 13-5-6
 * Time: 下午12:29
 * To change this template use File | Settings | File Templates.
 */
public class UserManageTest {
    @Test
    public void testUserManage(){
        Mockery mockery= new Mockery();
        final UserService userService=mockery.mock(UserService.class);
        final String message="abin";
        final String expectValue="hello abin";
         mockery.checking(new Expectations(){{
             oneOf(userService).getMessage(message);
             will(returnValue(expectValue));
         }});
        String actual=userService.getMessage(message);
        System.out.println("actual="+actual);
        TestCase.assertEquals(expectValue,actual);
        mockery.assertIsSatisfied();
    }
}








第二部分:JMOCK测试普通类

package com.abin.lee.mock.jmock;

/**
 * Created with IntelliJ IDEA.
 * User: abin
 * Date: 13-5-6
 * Time: 下午12:43
 * To change this template use File | Settings | File Templates.
 */
public class UserManage {
    public String getMessage(String message){
       String result="hello "+message;
        System.out.println("result="+result);
        return result;
    }
}

 






package com.abin.lee.mock.jmock;

import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;

/**
 * Created with IntelliJ IDEA.
 * User: abin
 * Date: 13-5-6
 * Time: 下午12:47
 * To change this template use File | Settings | File Templates.
 */
public class UserManageTest {
    @Test
    public void testUserManage(){
        Mockery mockery=new Mockery();
        mockery.setImposteriser(ClassImposteriser.INSTANCE);
        final UserManage userManage=mockery.mock(UserManage.class);
        final String message="abin";
        final String expectValue="hello abin";
        mockery.checking(new Expectations(){{
            oneOf(userManage).getMessage(message);
            will(returnValue(expectValue));
        }});
        String expect=userManage.getMessage(message);
        System.out.println("expect="+expect);
        TestCase.assertEquals(expectValue,expect);
        mockery.assertIsSatisfied();
    }
}







posted on 2013-05-06 12:42 abin 阅读(601) 评论(0)  编辑  收藏 所属分类: JMOCK

只有注册用户登录后才能发表评论。


网站导航: