如何使用 EasyMock 模拟 .equal() 方法

huangapple go评论70阅读模式
英文:

How To Mock a .equal() method using easyMock

问题

以下是翻译好的内容:

我有以下的代码片段要在 if 块内进行测试:

if ("anyString".equals("anyString")) {
    //body
}

我该如何使用 EasyMock 进行上述测试,方法如下:

假设 Chair 是基类,有一个返回其名称的 getName() 方法:

expect(chair.getName()).andReturn("string");
这会抛出 InvocationTargetException 异常

任何帮助将不胜感激
英文:

I have the following code segment to test inside if block

   if("anyString".equals("anyString")){
      //body
    }

How can I test above using easy mock as follows

lets take Chair as base class and getName() method which returns it name

expect(chair.getName()..eq("string")).andReturn(true);

it's throw an InvocationTargetException

Any Help Appreciated

答案1

得分: 2

这是两个字符串。嘲笑一个String没有意义。您的expect目前没有意义。请参阅EasyMock文档

如果我们说chair是一个模拟,并且您希望getName()返回string,那么代码将如下所示:expect(chair.getName()).andReturn("string");

我认为这与您的问题无关,但请注意equals无法被模拟。这是EasyMock在内部使用的特殊方法。equalstoStringhashCode无法被模拟。

英文:

These are two strings. There is no point in mocking a String. Your expect currently doesn't make sense Please see the EasyMock documentation.

If we say that chair is a mock, and you want getName() to return string, it would be expect(chair.getName()).andReturn("string");

I don't think it is relevant to your question but not that equals can't be mocked. It's a special method used by EasyMock internally. equals, toString and hashCode can't be mocked.

huangapple
  • 本文由 发表于 2020年10月7日 18:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定