EasyMock学习

EasyMock Home
  • http://easymock.org
  • easymock-2.4.jar 只能对接口进行Mock, 如果需要Mock类,则要下载easymockclassextension-2.4.jar

EasyMock 为接口动态的创建Mock对象步骤
  • 使用EasyMock生成Mock对象
  • 设定Mock对象的预期行为和输出
  • 将Mock对象切换到Replay状态
  • 调用Mock对象方法进行单元测试
  • 对Mock对象的行为进行验证

EasyMock Test
package com.taobao;

import static com.taobao.matcher.SQLMatch.sqlMatch;
import static org.easymock.EasyMock.createControl;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easymock.IMocksControl;
import org.junit.Test;

/**
 * Jeck's EasyMock Test 
 * 
 * @author jeck.xie 2009-3-25
 */
public class EasyMockTest {
	
	HttpServletRequest reqMock;
	HttpServletResponse rspMock;
	
	Statement mockStatement;
	ResultSet mockResultSet;
	
	@Test
	public void testMock() {
		// 1.使用EasyMock生成Mock对象
		reqMock = createMock(HttpServletRequest.class);
		rspMock = createMock(HttpServletResponse.class);
		mockStatement = createMock(Statement.class);
		mockResultSet = createMock(ResultSet.class);
		
		// 2.设定Mock对象的预期行为和输出
		expect(reqMock.getProtocol()).andReturn("HTTP/1.1").times(1);
		expect(reqMock.getMethod()).andReturn("GET"); //调用次数 times(1) default
		expect(reqMock.getParameter("jeck.key")).andReturn("jeck.value");
		expect(rspMock.getLocale()).andThrow(new RuntimeException()).anyTimes();
		rspMock.getContentType();
		expectLastCall().andReturn("text/xml;charset=UTF-8").atLeastOnce();
		
		try {
			// 参数匹配器
			mockStatement.executeQuery(sqlMatch("SELECT * FROM mytable"));
			expectLastCall().andStubReturn(mockResultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 3.将Mock对象切换到 Replay状态
		replay(reqMock, rspMock);
		
		// 4.调用Mock对象方法进行单元测试
		assertEquals(rspMock.getContentType(), "text/xml;charset=UTF-8");
		assertEquals(reqMock.getMethod(), "GET");
		assertEquals(reqMock.getParameter("jeck.key"), "jeck.value");
		assertEquals(reqMock.getProtocol(), "HTTP/1.1");
		
		// 5.对Mock对象的行为进行验证
		verify(reqMock, rspMock);
	}
	
	/**
	 * 用IMocksControl创建、管理Mock对象
	 */
	@Test
	public void testIMocksControl() {
		// 1.用IMocksControl创建多个Mock对象
		IMocksControl control = createControl();
		reqMock = control.createMock(HttpServletRequest.class);
		rspMock = control.createMock(HttpServletResponse.class);
		mockStatement = control.createMock(Statement.class);
		mockResultSet = control.createMock(ResultSet.class);
		
		// 3.将IMocksControl接口生成的Mock对象全部切换成Replay状态
		control.replay();
		
		// 5.同上
		control.verify();
	}

}

package com.taobao.matcher;

import static org.easymock.EasyMock.reportMatcher;

import org.easymock.IArgumentMatcher;

/**
 * 在EasyMock中使用参数匹配器<br>
 * 自定义参数匹配器
 * 
 * @author jeck.xie 2009-3-25
 */
public class SQLMatch implements IArgumentMatcher {

	private String regSQL = null;

	public SQLMatch(String expectedSQL) {
		this.regSQL = expectedSQL;
	}

	public void appendTo(StringBuffer buffer) {
		buffer.append("sqlMatch(\"" + regSQL + "\")");
	}

	public boolean matches(Object matchSQL) {
		if (matchSQL == null && regSQL == null)
			return true;
		else if (matchSQL instanceof String)
			return regSQL.equalsIgnoreCase((String) matchSQL);
		else
			return false;
	}

	public static String sqlMatch(String in) {
		reportMatcher(new SQLMatch(in));
		return in;
	}

}

你可能感兴趣的:(sql,xml,servlet,JUnit,单元测试)