Actual Result of testExecute() is "Missing record for Gear E2. Cannot mark this item arrival." ----- package org.example; import org.example.cmd.CmdArrive; import org.example.day.SystemDate; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.reflect.Field; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.*; class CmdArriveTest { private CmdArrive cmd; private Library library; private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); @BeforeEach public void resetInstance() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field systemDateInstance = SystemDate.class.getDeclaredField("instance"); systemDateInstance.setAccessible(true); systemDateInstance.set(null, null); Field instance = Library.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, null); SystemDate.createTheInstance("01-Jan-2020"); library = Library.getInstance(); Field field = library.getClass().getDeclaredField("allMembers"); field.setAccessible(true); ArrayList<Member> allMembers = (ArrayList<Member>) field.get(library); allMembers.clear(); Field field2 = library.getClass().getDeclaredField("allGears"); field2.setAccessible(true); ArrayList<Member> allGears = (ArrayList<Member>) field2.get(library); allGears.clear(); System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @BeforeEach public void setup() { cmd = new CmdArrive(); } @Test public void testExecute() { cmd.execute(new String[] {"arrive", "E2"}); String expected = """ Done. """; assertEquals(expected, outContent.toString()); }
The problem should because we didn't new a Gear class to add the Gear first.
Change the test case as follows, new a Gear class to add the E1 Printer first: ---- @Test public void testExecute() { Gear e = new Gear("E1", "Printer"); cmd.execute(new String[] {"arrive", "E1"}); String expected = """ Done. """; assertEquals(expected, outContent.toString()); } --- Also add a test case to test the ExecuteErrorExGearNotFound exception --- @Test public void testExecuteErrorExGearNotFound() { String[] args = {"arrive", "E2"}; cmd.execute(args); String expected = """ Missing record for Gear E2. Cannot mark this item arrival. """; assertEquals(expected, outContent.toString()); }