简体   繁体   中英

How To Mock a .equal() method using easyMock

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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