EasyMock:andAnswer()与andDelegateTo() [英] EasyMock: andAnswer() vs andDelegateTo()

查看:148
本文介绍了EasyMock:andAnswer()与andDelegateTo()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EasyMock中andAnswer()andDelegateTo()方法在用法方面有哪些区别?

Which are differences between andAnswer() and andDelegateTo() methods in EasyMock in terms of usage?

第一个区别

我知道使用andAnswer方法时,将跳过构造函数调用.如果构造函数需要做其他事情,这一点很重要.

I know that when andAnswer method is used, it is skipped the constructor call. This is important if the constructor makes extra things.

class Dummy {
    public Dummy(Object someArgument) {
        // some validations of arguments
        System.out.println("the constructor is called");
    }
    public Object method() {
        System.out.println("the method is called");
        return new Object();
    }
}


@Test
public void testSt1() {
    Dummy mock = EasyMock.createMock(Dummy.class);
    EasyMock.expect(mock.method()).andAnswer(new IAnswer<Object>() {
        @Override
        public Object answer() throws Throwable {
            System.out.println("mocked method is called");
            return new Object();
        }
    } );

    EasyMock.replay(mock);
    mock.method();
}

@Test
public void testSt2() {
    Dummy mock = EasyMock.createMock(Dummy.class);
    EasyMock.expect(mock.method()).andDelegateTo(new Dummy(new Dummy(new Object()) {
        @Override
        public Object method() {
            System.out.println("mocked method is called");
            return new Object();
        }
    } );

    EasyMock.replay(mock);
    mock.method();
}

结果:

  • testSt1()不调用Dummy
  • 的构造函数
  • testSt2()调用Dummy
  • 的构造函数
  • testSt1() does not call the constructor of Dummy
  • testSt2() calls the constructor of Dummy

其他区别是什么?

推荐答案

这两种方法的目的是为您的测试提供不同级别的责任.不过,您的示例并不是很好.

The purpose of the two methods is to provide different levels of responsibility for your tests. Your example isn't that great, though.

这是一个简单的方法,可以演示这两者在功能上如何提供不同的测试期望.

Here's a simple method that demonstrates how functionally these two provide different test expectations.

public String foo() throws Exception {
    throw new Exception();
}

使用andAnswer,您可以使此方法的模拟版本返回String,即使实际上它永远不会返回String.您使用andAnswer表示预期的响应.

With andAnswer, you can make a mocked version of this method return a String, even though it would never return one in practice. Your use of andAnswer implies an expected response.

对于andDelegateTo,这将始终引发异常.您使用andDelegateTo表示实际的反应.

With andDelegateTo, this will always throw an Exception. Your use of andDelegateTo implies an actual response.

andAnswer表示您特定于测试的代码将处理响应.例如,如果您为MockDao update方法创建ParrotAnswer,则Parrot将返回更新的Object,但是在此过程中实际上没有实例化Dao.这对于单元测试非常有用,因为您基本上可以遍历测试主题,但是如果您模拟的方法没有像您实际执行的那样,则无济于事.

andAnswer means your test-specific code will handle the response. For example, if you create a ParrotAnswer for a MockDao update method, the Parrot will return the updated Object, but no Dao is actually instantiated in the process. This is nice for unit testing where you basically walk the test subject through, but doesn't help if your mocked method doesn't do as what you method actually does.

andDelegateTo允许您提供实现接口以处理响应的实际Object.我们允许测试对象控制对资源的访问,而不是提供对完整资源的无限制访问.这样做的好处是,您可以测试集成到测试环境中的内容,但可以最大程度地减少对测试环境的实际更改.例如,您可以将get委托给有线Dao以从Db中获取实际的实时值,并模拟delete方法,因此您实际上不必在测试期间删除相同的值(并且不必再次创建它)例如,稍后再执行相同的测试(如果它具有静态ID).

andDelegateTo allows you to provide an actual Object implementing the interface to handle the response. We're allowing our test subject controlled access to a resource, rather than providing unrestricted access to a full resource. benefit of this is that you can test integration into a test environment, but minimize actual changes to the test environment. For example, you can delegate get to a wired Dao to fetch an actual live value from the Db, and mock the delete method, so you don't actually delete that same value during testing (and having to recreate it again later to do the same test if it has a static id, for example).

这篇关于EasyMock:andAnswer()与andDelegateTo()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆