diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.java index 88e005c..ad25467 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.java @@ -39,6 +39,7 @@ import org.testng.annotations.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * The GoogleSearchTest class. @@ -68,26 +69,14 @@ public class GoogleSearchTest extends LocalProperties { assertThat(messages).as("aapl results not empty").isNotEmpty(); assertThat(messages.get(0).getMessage()).as("found apple").containsIgnoringCase("apple"); - try { - GoogleSearch.searchGoogle("test", "", "apiKey"); - } catch (Exception e) { - assertThat(e).as("no API key").isInstanceOf(ModuleException.class); - assertThat(e).as("no API key exception has no cause").hasNoCause(); - } + assertThatThrownBy(() -> GoogleSearch.searchGoogle("test", "", "apiKey")).as("no API key").isInstanceOf( + ModuleException.class).hasNoCause(); - try { - GoogleSearch.searchGoogle("test", "apiKey", ""); - } catch (Exception e) { - assertThat(e).as("no CSE API key").isInstanceOf(ModuleException.class); - assertThat(e).as("no CSE API key exception has no cause").hasNoCause(); - } + assertThatThrownBy(() -> GoogleSearch.searchGoogle("test", "apiKey", "")).as("no CSE API key").isInstanceOf( + ModuleException.class).hasNoCause(); - try { - GoogleSearch.searchGoogle("", "apikey", "apiKey"); - } catch (Exception e) { - assertThat(e).as("no query").isInstanceOf(ModuleException.class); - assertThat(e).as("no query exception has no cause").hasNoCause(); - } + assertThatThrownBy(() -> GoogleSearch.searchGoogle("", "apikey", "apiKey")).as("no query").isInstanceOf( + ModuleException.class).hasNoCause(); } catch (ModuleException e) { // Avoid displaying api keys in CI logs. if ("true".equals(System.getenv("CI"))) { diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java index 488de37..4b6e665 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java @@ -39,6 +39,7 @@ import org.testng.annotations.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * The StockQuoteTest class. @@ -62,19 +63,11 @@ public class StockQuoteTest extends LocalProperties { messages = StockQuote.getQuote("012", apiKey); assertThat(messages.get(0).isError()).as("invalid symbol error").isTrue(); - try { - StockQuote.getQuote("test", ""); - } catch (Exception e) { - assertThat(e).as("no API key").isInstanceOf(ModuleException.class); - assertThat(e).as("no API key exception has no cause").hasNoCause(); - } + assertThatThrownBy(() -> StockQuote.getQuote("test", "")).as("no API key").isInstanceOf( + ModuleException.class).hasNoCause(); - try { - StockQuote.getQuote("", "apikey"); - } catch (Exception e) { - assertThat(e).as("no symbol").isInstanceOf(ModuleException.class); - assertThat(e).as("no symbol exception has no cause").hasNoCause(); - } + assertThatThrownBy(() -> StockQuote.getQuote("", "apikey")).as("no symbol").isInstanceOf( + ModuleException.class).hasNoCause(); } catch (ModuleException e) { // Avoid displaying api keys in CI logs. diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java b/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java index b347fc2..b26e7ad 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java @@ -33,12 +33,14 @@ package net.thauvin.erik.mobibot.modules; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import net.aksingh.owmjapis.api.APIException; import net.thauvin.erik.mobibot.msg.Message; import org.testng.annotations.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * The Weather2Test class. @@ -59,19 +61,17 @@ public class Weather2Test extends LocalProperties { assertThat(messages.get(0).getMessage()).as("is UK").contains("UK"); assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4298960"); - try { - Weather2.getWeather("test", ""); - } catch (Exception e) { - assertThat(e).as("no API key").isInstanceOf(ModuleException.class); - assertThat(e).as("no API key exception has no cause").hasNoCause(); - } + assertThatThrownBy( + () -> Weather2.getWeather("Montpellier, FR", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP))).as( + "Montpellier not found").hasCauseInstanceOf(APIException.class); + + assertThatThrownBy( + () -> Weather2.getWeather("test", "")).as( + "no API key").isInstanceOf(ModuleException.class).hasNoCause(); + + messages = Weather2.getWeather("", "apikey"); + assertThat(messages.get(0).isError()).as("No api key.").isTrue(); - try { - Weather2.getWeather("", "apikey"); - } catch (Exception e) { - assertThat(e).as("no query").isInstanceOf(ModuleException.class); - assertThat(e).as("no query exception has no cause").hasNoCause(); - } } @Test