I need to throw MQTTException when the MQTTClient constructor is called, as shown below in the code.
public MqttClient createClient(String url, String clientId) throws ServiceSDKException {
if (!(url.isEmpty() || url == null || clientId.isEmpty() || clientId == null)) {
try {
client = new MqttClient(url, clientId);
} catch (MqttException e) {
throw new ServiceSDKException("MqttException occurred when creating a new client", e);
} catch (IllegalArgumentException e) {
throw new ServiceSDKException("Illegal arguments detected, could be malformed url", e);
}
} else {
throw new ServiceSDKException("URL and ClientID cannot be null or empty");
}
return client;
}
@Override
@PrepareForTest(MqttClient.class)
@Test(expected = ServiceSDKException.class)
public void should_throw_ServiceSDKException_when_MqttException_occurs_while_creating_the_client() throws Exception {
PowerMockito.whenNew(MqttClient.class).withArguments(Matchers.anyString(), Matchers.anyString()).thenThrow(new MqttException(Mockito.mock(Throwable.class)));
MqttClient client = clientMqtt.createClient("tcp://localhost:1883", "100");
}
I have added the below line to the class as well.
@RunWith(PowerMockRunner.class)
But I am getting an assertion error saying my exception was not thrown. I tried debugging and it doesn't throw MQTTException as I expect, when creating the client instance. What am I doing wrong here? How do I test this line? Please advice.
Aucun commentaire:
Enregistrer un commentaire