From bf95764075b3e581ebe459d4357cfc389d17b59e Mon Sep 17 00:00:00 2001 From: atayal Date: Fri, 5 Nov 2021 19:06:03 +0530 Subject: [PATCH] sonar fix - add test cases for CommanderTest --- .../com/iluwatar/commander/CommanderTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/commander/src/test/java/com/iluwatar/commander/CommanderTest.java b/commander/src/test/java/com/iluwatar/commander/CommanderTest.java index 4091b1054..f4da94a3f 100644 --- a/commander/src/test/java/com/iluwatar/commander/CommanderTest.java +++ b/commander/src/test/java/com/iluwatar/commander/CommanderTest.java @@ -4,6 +4,7 @@ import com.iluwatar.commander.employeehandle.EmployeeDatabase; import com.iluwatar.commander.employeehandle.EmployeeHandle; import com.iluwatar.commander.exceptions.DatabaseUnavailableException; import com.iluwatar.commander.exceptions.ItemUnavailableException; +import com.iluwatar.commander.exceptions.PaymentDetailsErrorException; import com.iluwatar.commander.exceptions.ShippingNotPossibleException; import com.iluwatar.commander.messagingservice.MessagingDatabase; import com.iluwatar.commander.messagingservice.MessagingService; @@ -20,6 +21,7 @@ import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; class CommanderTest { @@ -31,6 +33,16 @@ class CommanderTest { private final long messageTime = 5_000; private final long employeeTime = 2_000; + private static List exceptionList = new ArrayList<>(); + + static { + exceptionList.add(new DatabaseUnavailableException()); + exceptionList.add(new ShippingNotPossibleException()); + exceptionList.add(new ItemUnavailableException()); + exceptionList.add(new PaymentDetailsErrorException()); + exceptionList.add(new IllegalStateException()); + } + private Commander buildCommanderObject() { return buildCommanderObject(false); } @@ -301,13 +313,7 @@ class CommanderTest { @Test void testPlaceOrderWithDatabaseAndExceptions() throws Exception { - List l = new ArrayList(); - l.add(new DatabaseUnavailableException()); - l.add(new ShippingNotPossibleException()); - l.add(new ItemUnavailableException()); - l.add(new IllegalStateException()); - - for (Exception e : l) { + for (Exception e : exceptionList) { Commander c = buildCommanderObjectWithDB(true, true, e); var order = new Order(new User("K", null), "pen", 1f); @@ -342,12 +348,7 @@ class CommanderTest { @Test void testPlaceOrderWithoutDatabaseAndExceptions() throws Exception { - List l = new ArrayList<>(); - l.add(new ShippingNotPossibleException()); - l.add(new ItemUnavailableException()); - l.add(new IllegalStateException()); - - for (Exception e : l) { + for (Exception e : exceptionList) { Commander c = buildCommanderObjectWithoutDB(true, true, e); var order = new Order(new User("K", null), "pen", 1f);