In my class TwitchService I'm creating a private field twitchClient using TwitchClientBuilder. How can I approach testing method sendChatMessage() which is using twitchClient?
public class TwitchService {
public TwitchService(){
twitchClient = TwitchClientBuilder.builder().build();
}
public void sendChatMessage(String content) throws Exception {
if(content.isEmpty()){
throw new Exception("Cannot send empty message!");
}
twitchClient.getChat().sendMessage(content);
}
}
So far, I have tried mocking TwitchClientBuilder.builder() and twitchCliendBuilder.build() like this:
@RunWith(MockitoJUnitRunner.class)
public class TwitchServiceTest {
private TwitchService twitchService;
@Mock
private TwitchClientBuilder twitchClientBuilder;
@Test
public void sendChatMessage() throws Exception {
try (MockedStatic<TwitchClientBuilder> twitchClientBuilderMockedStatic = Mockito.mockStatic(TwitchClientBuilder.class)){
twitchClientBuilderMockedStatic.when(() -> TwitchClientBuilder.builder()).thenReturn(mock(TwitchClientBuilder.class));
when(twitchClientBuilder.build()).thenReturn(mock(TwitchClient.class));
twitchService = new TwitchService("x", "y");
twitchService.sendChatMessage("test message");
}
}
}
But I get NullPointerException on twitchClient in sendChatMessage() Any ideas how to properly mock fields like this?
Aucun commentaire:
Enregistrer un commentaire