vendredi 30 octobre 2015

How to verify ThreadPool execute method which takes Lambda as parameter

I am currently testing a method in which I execute a runnable into a ThreadPool.

Here is the code for the method.

@Override
public void queueLogin(Connection connection, LoginPacket packet) {
    if(isRunning)
        loginPool.execute(() -> loginService.tryLogin(connection, packet));
}

Now I need to verify whether the execute method was invoked or not with the correct parameters.

Here is my test case

@Test
public void theQueueLoginShouldCallExecuteOnThreadPoolIfManagerIsRunning() {
    //Arrange
    loginManager.start();

    //Act
    loginManager.queueLogin(moqConnection, moqLoginPacket);

    //Assert
    verify(moqLoginPool).execute(() -> moqLoginService.tryLogin(moqConnection, moqLoginPacket));
}

But the test is failing saying

Argument(s) are different! Wanted:
executorService.execute(
    com.battletanks.game.server.unittests.manager.login.LoginManagerTests$$Lambda$2/1668016508@3d51f06e
);
-> at com.battletanks.game.server.unittests.manager.login.LoginManagerTests.theQueueLoginShouldCallExecuteOnThreadPoolIfManagerIsNotRunning(LoginManagerTests.java:84)
Actual invocation has different arguments:
executorService.execute(
    com.battletanks.game.server.manager.login.DefaultLoginManager$$Lambda$1/77269878@7ed7259e
);

I understand what is wrong here but I am not sure how to fix this.

Aucun commentaire:

Enregistrer un commentaire