2

I just written sample test case, where I would like to mock a void instance method. I am surprised, my test case is passing without calling the expectLastCall method. I would like to know, is calling of expectLastCall is not required while mocking instance void methods?

StringUtil.java

package com.sample.util;

import com.sample.model.MethodNotImplementedException;

public class StringUtil {
    public String toUpperAndRepeatStringTwice(String str) {
        String upperCase = str.toUpperCase();
        sendStringToLogger(upperCase);
        return upperCase + upperCase;
    }

    public void sendStringToLogger(String str){
        throw new MethodNotImplementedException();
    }
}

StringUtilTest.java

package com.sample.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {

    @Test
    public void toUpperAndRepeatStringTwice() {
        StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, "sendStringToLogger");

        String str = "HELLO PTR";
        stringUtil.sendStringToLogger(str);
        //PowerMock.expectLastCall().times(1);
        PowerMock.replayAll();

        String result = stringUtil.toUpperAndRepeatStringTwice("hello ptr");

        assertEquals(result, "HELLO PTRHELLO PTR");
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
  • I think you will find the answer [here](http://stackoverflow.com/questions/22831523/easymock-void-method) – Fady Saad May 05 '17 at 04:23
  • Just curious: What are you using PowerMock for here? Mockito should be able to mock or spy your `StringUtil` quite fine without having resorting to PowerMock... Am I missing a use case here? – Florian Schaetz May 05 '17 at 06:05
  • I am using PowerMock and EasyMock. Just to experiment – Hari Krishna May 05 '17 at 06:07
  • @HariKrishna Don't. Only use PowerMock if you absolutely have to (because you have to test some 3rd party code you can't change). If you are *experimenting* / *learning* ... then suggestion: go for Mockito or pure EasyMock. Learn how to use these frameworks properly, and learn how to write easy-to-test code. – GhostCat May 05 '17 at 09:01
  • Thanks @GhostCat, but if i am not wrong, PowerMock is an extension of other Mocking frameworks like Mockito or EasyMock. We can use either PowerMock+EasyMock (or) PowerMock+Mockito right. Correct me If I am wrong. – Hari Krishna May 05 '17 at 09:40
  • PowerMock is an **extension** to EasyMock; and PowerMockito to Mockito. But understand: the Powerxxx things do byte code manipulation. That is what enables Powerxxx to mock static/new calls. And just for the record: PowerMockito uses outdated versions of Mockito; as Mockito itself is at version 2.8 by now; but the "best" you can with PowerMockito is 2.0.40beta something. – GhostCat May 05 '17 at 09:43
  • PowerMock is mostly used to deal with crappy code, for example static calls, etc. As long as Mockito/EasyMock is sufficient, use that and do not burden yourself with PowerMock(ito). If your own code requires PowerMock(ito) then refactor your code. Only if you cannot do that, for example because of external libraries that make it necessary, and you really, really cannot find another way around it, use PowerMock(ito). – Florian Schaetz May 05 '17 at 13:04
  • @GhostCat you have not up to date information. PowerMock works with Mockito 2. At least with Mockito 2.7.5 – Artur Zagretdinov May 05 '17 at 14:16
  • @ArthurZagretdinov When I tried 4 weeks ago, it didn't. And when I downloaded PowerMockito, it came with 2.042beta. If that has changed, neither their [home page](https://github.com/powermock/powermock/wiki/Mockito-2-(Maven)) nor their [bug tracker](https://github.com/powermock/powermock/issues/726) is making that official. – GhostCat May 05 '17 at 14:29

1 Answers1

4

expectLastCall is not required. For EasyMock and the PowerMock layer as well. So you are right.

It is used for clarity for some users. Because it makes it obvious that the method before is an expectation not some random call. But it is more a question of style than a requirement.

You also don't need the time(1) since it is the default.

BTW, the answer here is wrong and I've commented on it accordingly.

Community
  • 1
  • 1
Henri
  • 5,551
  • 1
  • 22
  • 29
  • Even when you do **verify** calls? – GhostCat May 06 '17 at 07:46
  • Yes. It makes no difference. Calling a void method in recording mode records it. – Henri May 06 '17 at 17:55
  • I was using EasyMock for several years by now; and today I learn that I only need `expectLastCall()` when I need to call "follow on" methods like `times()` or `andThrow()`. Teaching me that lesson makes you a guest in todays Ghostcat-upvote-party! – GhostCat May 06 '17 at 18:58