Encapsulated errors in IllegalStateException

getList() and getStops() no longer silently ignore IOExceptions and also
do not print StackTraces any more.
This commit is contained in:
Stefan Kalscheuer 2017-08-29 18:24:44 +02:00
parent ca9578a4d3
commit 6b0481a5e1
2 changed files with 17 additions and 8 deletions

View File

@ -233,7 +233,7 @@ public class UraClient {
line = br.readLine(); line = br.readLine();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IllegalStateException("Failed to read from API", e);
} }
return trips; return trips;
} }
@ -268,7 +268,7 @@ public class UraClient {
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IllegalStateException("Failed to read from API", e);
} }
return stops; return stops;
} }

View File

@ -32,6 +32,7 @@ import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
@ -69,9 +70,13 @@ public class UraClientTest {
throw new IOException("Provoked exception 1."); throw new IOException("Provoked exception 1.");
} }
}); });
assertThat(new UraClient("mocked").getStops(), hasSize(0)); try {
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2.")); new UraClient("mocked").getStops();
assertThat(new UraClient("mocked").getStops(), hasSize(0)); } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1."));
}
} }
@Test @Test
@ -221,9 +226,13 @@ public class UraClientTest {
throw new IOException("Provoked exception 1."); throw new IOException("Provoked exception 1.");
} }
}); });
assertThat(new UraClient("mocked").getTrips(), hasSize(0)); try {
PowerMockito.when(mockURL.openStream()).thenThrow(new IOException("Provoked exception 2.")); new UraClient("mocked").getTrips();
assertThat(new UraClient("mocked").getTrips(), hasSize(0)); } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1."));
}
} }
@Test @Test