Junit - JMock 入门

1.JMock 入门

官网:http://jmock.org/getting-started.html

所用版本:jmock-2.6.0.jar

引用jar包:

2.实例

2.1 接口服务

package com.weizhi.jmock.demo;

/**
 * @ClassName IService
 * @Description TODO
 * @Author weizhi2018 
 * @Date 2013-10-22 下午02:25:58 
 *
 */

public interface IService {
	public String service(String data);
}

2.2 测试用例

package com.weizhi.jmock.demo;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ServiceTest extends TestCase{
	private Mockery context = null;
	private IService service = null;

	@Before
	public void setUp() throws Exception {
		context = new Mockery();
		service = context.mock(IService.class);
		
		final String data = "weizhi2018";
		
		context.checking(new Expectations(){
			{
				 allowing(service).service(data);//支持两次调用
//				will(returnValue("true"));
				 will(onConsecutiveCalls( 
	                       returnValue("true"), 
	                       returnValue("false"))); 
			}
		});
	}


	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for
	 * {@link com.weizhi.jmock.demo.IService#service(java.lang.String)}.
	 */
	@Test
	public void testService() {
		
		assertEquals(service.service("weizhi2018"), "true");
		assertEquals(service.service("weizhi2018"), "false");
//		assertEquals(service.service("weizhi2018"), "true");
	}

}

如果第三次调用
//		assertEquals(service.service("weizhi2018"), "true");
去掉注释,就会报错:

no more actions available: iService.service("weizhi2018")
expectations:
  allowed, already invoked 3 times: iService.service("weizhi2018"); returns "true", and then returns "false"
      parameter 0 matched: "weizhi2018"
what happened before this:
  iService.service("weizhi2018")
  iService.service("weizhi2018")

	at org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)
	at org.jmock.lib.action.ActionSequence.invoke(ActionSequence.java:34)
	at org.jmock.internal.InvocationExpectation.invoke(InvocationExpectation.java:165)
	at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:81)
	at org.jmock.Mockery.dispatch(Mockery.java:231)
	at org.jmock.Mockery.access$100(Mockery.java:29)
	at org.jmock.Mockery$MockObject.invoke(Mockery.java:271)
	at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
	at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
	at org.jmock.internal.SingleThreadedPolicy$1.invoke(SingleThreadedPolicy.java:21)
	at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
	at com.sun.proxy.$Proxy0.service(Unknown Source)
	at com.weizhi.jmock.demo.ServiceTest.testService(ServiceTest.java:80)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值