简体   繁体   English

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

[英]How To Mock a .equal() method using easyMock

I have the following code segment to test inside if block我有以下代码段来测试 if 块

   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让我们以 Chair 作为基础 class 和返回它名称的 getName() 方法

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

it's throw an InvocationTargetException它抛出一个 InvocationTargetException

Any Help Appreciated任何帮助表示赞赏

These are two strings.这是两个字符串。 There is no point in mocking a String. mocking 没有意义的一个String。 Your expect currently doesn't make sense Please see the EasyMock documentation .您的expect目前没有意义请参阅EasyMock 文档

If we say that chair is a mock, and you want getName() to return string , it would be expect(chair.getName()).andReturn("string");如果我们说chair是一个 mock,而你希望getName()返回string ,那就是expect(chair.getName()).andReturn("string");

I don't think it is relevant to your question but not that equals can't be mocked.我认为这与您的问题无关,但不能嘲笑equals It's a special method used by EasyMock internally.这是 EasyMock 内部使用的一种特殊方法。 equals , toString and hashCode can't be mocked. equalstoStringhashCode不能被模拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM