junit4 mockito中的什么是easymock

博客分类:
junit 4,easymock 3
1、测试代码
import static org.junit.Assert.assertE
import java.io.IOE
import javax.servlet.RequestD
import javax.servlet.ServletC
import javax.servlet.ServletE
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.easymock.EasyM
import org.junit.B
import org.junit.T
public class SampleServletTest {
private SampleS
private HttpServletRequest mockR
private HttpServletResponse mockR
private ServletContext mockServletC
private RequestDispatcher mockD
public void setUp() throws Exception {
mockRequest = EasyMock.createMock(HttpServletRequest.class);
mockRespones = EasyMock.createMock(HttpServletResponse.class);
mockServletContext = EasyMock.createMock(ServletContext.class);
mockDispathcher = EasyMock.createMock(RequestDispatcher.class);
servlet = new SampleServlet() {
private static final long serialVersionUID = 1L;
public ServletContext getServletContext() {
return mockServletC
public void testDoGetHttpServletRequestHttpServletResponse() throws ServletException, IOException {
EasyMock.expect(mockRequest.getParameter("username")).andReturn("test").times(1);
EasyMock.expect(mockRequest.getParameter("password")).andReturn("123456").times(1);
EasyMock.expect(mockServletContext.getRequestDispatcher("dispather")).andReturn(mockDispathcher).times(1);
mockDispathcher.forward(mockRequest, mockRespones);
EasyMock.expectLastCall();
EasyMock.replay(mockRequest, mockServletContext, mockDispathcher);
servlet.doGet(mockRequest, mockRespones);
EasyMock.verify(mockDispathcher);
public void testDoGetFailed() throws ServletException, IOException {
EasyMock.expect(mockRequest.getParameter("username")).andReturn("test").times(1);
EasyMock.expect(mockRequest.getParameter("password")).andReturn("123458").times(1);
EasyMock.replay(mockRequest);
servlet.doGet(mockRequest, mockRespones);
} catch (Exception e) {
assertEquals("Login error", e.getMessage());
EasyMock.verify(mockRequest);
2、实现代码
import java.io.IOE
import javax.servlet.RequestD
import javax.servlet.ServletC
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("test".equals(username) && "123456".equals(password)) {
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context
.getRequestDispatcher("dispather");
dispatcher.forward(request, response);
throw new RuntimeException("Login error");
donald3003a
浏览: 40385 次
来自: 成都
这举列有点不恰当啊。。电影人和不同类型的电影。是两个不同概念
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'如何使用Spring&+&EasyMock做Java单元测试
本文说明了如何使用Spring和EasyMock对Java代码进行单元测试,尤其是打桩(Stub)测试。本文选择JUnit作为Java的单元测试框架进行说明,如果使用其它的单元测试框架,其原理也是类似的。
在进行说明之前,先弄清楚几个概念:
桩函数:桩函数是用于单元测试中的特殊函数,是用来模拟被测试函数中调用的接口,要么令该函数返回空,要么返回固定的值。以使得被测函数能够正常测试其功能。这样有利于减少测试时测试环境的影响,或者简化测试环境,而能够完成被测试函数的单元测试。
Mock测试:mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。
Java中用于mock测试的框架有很多,其中EasyMock是比较简单,也是使用较为广范的一种mock框架,本文以EasyMock为例说明如何生成一个mock对象,并说明如何用Spring来将创建出的mock对象注入到我们要测试的真实对象中。
假设我们要测试OurClass中fun方法,但是fun方法依赖于OurClass中的other对象,因此在单元测试的过程中,我们希望other对象可以根据我们的需求返回特定的一些值,使得我们可以正常地测试OurClass对象的fun方法,因为毕竟这才是我们所关心的。因此,我们首先使用mock框架来生成一个虚拟的mock对象,再使用Spring将这个对象注入到被测试类中。
被测试类OurClass.java
public class OurClass {
private OtherClass other = new OtherClass();
public int fun() {
return other.fun();
被测试类依赖的类OtherClass:
public class OtherClass {
public int fun() {
单元测试代码:
import static org.junit.Assert.assertTrue;
import org.easymock.EasyM
import org.junit.T
import org.springframework.test.util.ReflectionTestU
public class OurClassTest {
public void testOurClass() {
OurClass classUnderTest = new OurClass();
assertTrue("fun should return
0", classUnderTest.fun() == 0);
OtherClass other = new OtherClass() {
&&&&&&&&&&
public int fun() {
&&&&&&&&&&&&&
&&&&&&&&&&
调用此函数将我们自己生成的一个对象注入到classUnderTest对象中,注意,即使该对象是
// private类型的也没有问题。
ReflectionTestUtils.setField(classUnderTest, "other", other);
assertTrue("fun should return
1", classUnderTest.fun() == 1);
创建Mock对象
OtherClass other2 =
EasyMock.mock(OtherClass.class);
对象的预期行为和输出
EasyMock.expect(other2.fun()).andReturn(2).times(1);
录制Mock对象的行为
EasyMock.replay(other2);
对象方法进行单元测试
ReflectionTestUtils.setField(classUnderTest, "other", other2);
assertTrue("fun should return
2", classUnderTest.fun() == 2);
对象的行为进行验证
EasyMock.verify(other2);
依赖的Java包(Gradle):
dependencies {
'org.springframework:spring-context:4.2.6.RELEASE'
compile 'org.springframework:spring-test:4.2.6.RELEASE'
testCompile "junit:junit:4.12"
testCompile
'org.springframework:spring-context:4.2.6.RELEASE'
testCompile
'org.springframework:spring-test:4.2.6.RELEASE'
testCompile 'org.easymock:easymock:3.4'
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 junit mock 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信