Je voudrais savoir comment je peux écrire un test unitaire pour obtenir le bloc catch pour la méthode suivante. Le FOM.create (données) est une méthode statique.Comment tester l'exception est lancée avec Junit
public String getValue(Data data) {
try {
return FOM.create(data);
} catch (UnsupportedEncodingException e) {
log.error("An error occured while creating data", e);
throw new IllegalStateException(e);
}
}
Actuellement ceci est mon test unitaire, mais il ne touche pas le bloc catch:
@Test (expected = UnsupportedEncodingException.class)
public void shouldThrowUnsupportedEncodingException() {
doThrow(UnsupportedEncodingException.class).when(dataService).getUpdatedJWTToken(any(Data.class));
try {
dataService.getValue(data);
}catch (IllegalStateException e) {
verify(log).error(eq("An error occured while creating data"), any(UnsupportedEncodingException.class));
throw e;
}
}
où est ce getUpdatedJWTToken dans votre code? – Plog