I am writing test for a method which takes in a String, but the String value is from a chain of method call.
// MyService myService = a wired global variable
public List<String> method() {
List<Message> messageList = myService.getMessages(UserCtx.currentUser().getUserInfo().getUserId());
// keep doing something else
}
UserCtx.currentUser().getUserInfo().getUserId() returns a String
Here is the unit test I wrote
myService = mock(MyService.class);
List<Message> dummyMessageList = new ArrayList<>();
when(myService.getMessages(anyString())).thenReturn(dummyMessageList);
List<String> result = myClass.method()
I am hoping that myService.getMessages will return dummyMessageList directly, but instead it goes into UserCtx.currentUser().getUserInfo().getUserId() and failed in the middle since there isn't a valid user context.
Can I put something like any() or anyString() in when() to bypass mocking UserCtx?
Or do I have to mock the chain UserCtx.currentUser().getUserInfo().getUserId() one by one?