diff --git a/kura/test/org.eclipse.kura.nm.test/src/test/java/org/eclipse/kura/nm/NetworkPropertiesTest.java b/kura/test/org.eclipse.kura.nm.test/src/test/java/org/eclipse/kura/nm/NetworkPropertiesTest.java index 1aa892f5122..e780c24239b 100644 --- a/kura/test/org.eclipse.kura.nm.test/src/test/java/org/eclipse/kura/nm/NetworkPropertiesTest.java +++ b/kura/test/org.eclipse.kura.nm.test/src/test/java/org/eclipse/kura/nm/NetworkPropertiesTest.java @@ -13,15 +13,18 @@ package org.eclipse.kura.nm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Optional; import org.eclipse.kura.configuration.Password; @@ -42,13 +45,12 @@ public class NetworkPropertiesTest { private List stringListResult; private Map resultMap; - private Boolean hasNullPointExceptionBeenThrown = false; - private Boolean hasNoSuchElementExceptionBeenThrown = false; + private Exception occurredException; @Test public void constructorShouldThrowWithNullMap() { givenNetworkPropertiesBuiltWith(null); - thenANullPointerExceptionOccured(); + thenExceptionOccurred(NullPointerException.class); } @Test @@ -91,7 +93,7 @@ public void getShouldThrowWithNullValue() { givenMapWith("testKey1", null); givenNetworkPropertiesBuiltWith(this.properties); whenGetIsCalledWith("testKey1", String.class); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -99,7 +101,7 @@ public void getShouldThrowWithMissingKey() { givenMapWith("testKey1", null); givenNetworkPropertiesBuiltWith(this.properties); whenGetIsCalledWith("testKey1-nonExistant", String.class); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -107,7 +109,7 @@ public void getShouldThrowWithEmptyString() { givenMapWith("Empty-String", ""); givenNetworkPropertiesBuiltWith(this.properties); whenGetIsCalledWith("Empty-String", String.class); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -115,7 +117,7 @@ public void getShouldThrowWithEmptyPassword() { givenMapWith("Empty-Password", new Password("")); givenNetworkPropertiesBuiltWith(this.properties); whenGetIsCalledWith("Empty-Password", Password.class); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -282,7 +284,7 @@ public void getStringListShouldThrowWithNullValue() { givenMapWith("testKey-comma-seperated", null); givenNetworkPropertiesBuiltWith(this.properties); whenGetStringListIsCalledWith("testKey-comma-seperated"); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -290,7 +292,7 @@ public void getStringListShouldThrowWithNonExistantKey() { givenMapWith("testKey-comma-seperated", null); givenNetworkPropertiesBuiltWith(this.properties); whenGetStringListIsCalledWith("testKey-comma-seperated-not-existant"); - thenANoSuchElementExceptionOccured(); + thenExceptionOccurred(NoSuchElementException.class); } @Test @@ -366,14 +368,10 @@ public void givenMapWith(String key, Object pair) { } public void givenNetworkPropertiesBuiltWith(Map properties) { - try { this.netProps = new NetworkProperties(properties); - - } catch (NullPointerException e) { - this.hasNullPointExceptionBeenThrown = true; - } catch (NoSuchElementException e) { - this.hasNoSuchElementExceptionBeenThrown = true; + } catch (Exception e) { + this.occurredException = e; } } @@ -404,42 +402,32 @@ public void whenGetIsCalledWith(String key, Class clazz) { throw new IllegalArgumentException("Data type is not supported with this Test"); } - } catch (NullPointerException e) { - this.hasNullPointExceptionBeenThrown = true; - } catch (NoSuchElementException e) { - this.hasNoSuchElementExceptionBeenThrown = true; + } catch (Exception e) { + this.occurredException = e; } } public void whenGetOptIsCalledWith(String key, Class clazz) { try { this.optResult = this.netProps.getOpt(clazz, key, ""); - } catch (NullPointerException e) { - this.hasNullPointExceptionBeenThrown = true; - } catch (NoSuchElementException e) { - this.hasNoSuchElementExceptionBeenThrown = true; + } catch (Exception e) { + this.occurredException = e; } } public void whenGetStringListIsCalledWith(String key) { try { this.stringListResult = this.netProps.getStringList(key, ""); - - } catch (NullPointerException e) { - this.hasNullPointExceptionBeenThrown = true; - } catch (NoSuchElementException e) { - this.hasNoSuchElementExceptionBeenThrown = true; + } catch (Exception e) { + this.occurredException = e; } } public void whenGetOptStringListIsCalledWith(String key) { try { this.optResult = this.netProps.getOptStringList(key, ""); - - } catch (NullPointerException e) { - this.hasNullPointExceptionBeenThrown = true; - } catch (NoSuchElementException e) { - this.hasNoSuchElementExceptionBeenThrown = true; + } catch (Exception e) { + this.occurredException = e; } } @@ -492,17 +480,22 @@ private void thenOptionalPasswordResultEquals(Optional optPasswordResu } } - public void thenANullPointerExceptionOccured() { - assertTrue(this.hasNullPointExceptionBeenThrown); - } - - public void thenANoSuchElementExceptionOccured() { - assertTrue(this.hasNoSuchElementExceptionBeenThrown); + private void thenExceptionOccurred(Class expectedException) { + assertNotNull(this.occurredException); + assertEquals(expectedException.getName(), this.occurredException.getClass().getName()); } public void thenNoExceptionsOccured() { - assertFalse(this.hasNullPointExceptionBeenThrown); - assertFalse(this.hasNoSuchElementExceptionBeenThrown); + String errorMessage = "Empty message"; + if (Objects.nonNull(this.occurredException)) { + StringWriter sw = new StringWriter(); + this.occurredException.printStackTrace(new PrintWriter(sw)); + + errorMessage = String.format("No exception expected, \"%s\" found. Caused by: %s", + this.occurredException.getClass().getName(), sw.toString()); + } + + assertNull(errorMessage, this.occurredException); } }