Skip to content
Advertisement

Testing method called with specific string

I am trying to find answer to my question but unable to find something working. I have a class which does this

public class LambdaLoggerWrapper {

    public LambdaLoggerWrapper(LambdaLogger lambdaLogger){
        this.lambdaLogger = lambdaLogger;
    }

    public void logInfo(String caller, String message){
        lambdaLogger.log(message);
    }
}

and my test is

@RunWith(MockitoJUnitRunner.class)
public class LambdaLoggerWrapperTest{
    @Mock LambdaLogger mockLambdaLogger;

    @Test
    public void testLog(){
         LambdaLoggerWrapper llw = new LambdaLoggerWrapper(mockLambdaLogger);
         //how to test that calling llw.logInfo actually calls lambdaLogger.log with appropriate string?
    }
}

How would I test that calling llw.logInfo actually calls lambdaLogger.log with the appropriate string?

Advertisement

Answer

Invoke the logInfo() method on the instance under test :

String message = "my message";
llw.logInfo("caller", message);

And use the Mockito#verify() method to assert that the mock was invoked with the expected method and with the parameter you have passed to the tested method :

Mockito.verify(lambdaLogger).log(message);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement