Precautions when using Mockito.anyString as an argument when Mocking with Mockito

Precautions when using Mockito.anyString as an argument when Mocking with Mockito

It's very easy, but I stumbled a little, so make a note of it.

//Mocking class
public class Mock {
    //Mock handling method
    public void mockMethod (String inString) {
        //processing
    }
}

When mocking the method as above, I was making the mock as below, But the result didn't work,

Hoge hoge = new Hoge();
Mockito.doReturn(hoge).when(mock).mockMethod(Mockito.anyString());


//Execution location
String inStr;
/**
There is some kind of kettle and processing ...
**/
Hoge hoge = mock.mockMethod(inStr);

If the hoge you receive is not the return value specified in the mock and you are worried The cause was simple, It was because the argument passed to Mock # mockMethod in the execution part was null ... If you specify Mockito.anyString (), null is not included. That's why I did this.

Hoge hoge = new Hoge();
Mockito.doReturn(hoge).when(mock).mockMethod(Mockito.any());

That's it.

Recommended Posts

Precautions when using Mockito.anyString as an argument when Mocking with Mockito
[Java] Precautions when converting variable-length argument parameters into an array
I got an InvalidUseOfMatchersException when using any with JUnit Mock
Precautions when using checkboxes in Thymeleaf
Validate arguments using ArgumentCaptor with mockito
Precautions when creating PostgreSQL with docker-compose
Invoke the character string passed as an argument as a method with send
Precautions when using querySelector () (such as some ideas when specifying the name attribute)
Precautions when using checkstyle version 6.6 or higher
Precautions when creating PostgreSQL with docker-compose
[Java] Precautions when comparing character strings with character strings
[Rails] Precautions when comparing date and time with DateTime
Precautions when generating a table with a composite key with Iciql + SQLite
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
Error when playing with java
[Ansible] Precautions when testing with docker driver with molecule v3.1 or later
Using hidden type when PUT with Thymeleaf
[Java] Precautions when comparing character strings with character strings
Be careful when using rails_semantic_logger with unicorn