### Question:
StartUI { public void init() { MenuTracker menu = new MenuTracker(this.input, this.tracker); menu.fillActions(); List<Integer> ranges = menu.getNumberMenuItems(); int key; do { System.out.println("Menu:"); menu.show(); key = this.input.ask("Select: ", ranges); menu.select(key); } while (key != menu.getExitKey()); } StartUI(Input input, Tracker tracker); void init(); static void main(String[] args);  }### Answer:
@Test public void whenUserAddItemThenTrackerHasNewItemWithSameName() { try (Tracker tracker = new Tracker()) { tracker.init(); Input input = new StubInput(new String[]{"0", "test name", "desc", "6"}); new StartUI(input, tracker).init(); assertThat(tracker.findAll().get(0).getName(), is("test name")); } }
@Test public void whenUpdateThenTrackerHasUpdatedValue() { try (Tracker tracker = new Tracker()) { tracker.init(); Item item = tracker.add(new Item()); Input input = new StubInput(new String[]{"2", item.getId(), "Y", "test name", "desc", "6"}); new StartUI(input, tracker).init(); assertThat(tracker.findById(item.getId()).getName(), is("test name")); } }
@Test public void whenDeleteItemThenItemNull() { try (Tracker tracker = new Tracker()) { tracker.init(); Item checked = tracker.add(new Item()); Input input = new StubInput(new String[]{"3", checked.getId(), "Y", "6"}); new StartUI(input, tracker).init(); assertNull(tracker.findById(checked.getId())); } }
@Test public void whenShowAllItemsThenOutputAllItems() { try (Tracker tracker = new Tracker()) { tracker.init(); Item first = new Item("test1", "testDesc1", 10L); Item second = new Item("test2", "testDesc2", 11L); first.setId("1"); second.setId("2"); tracker.add(first); tracker.add(second); Input input = new StubInput(new String[]{"1", "6"}); new StartUI(input, tracker).init(); assertThat( new String(out.toByteArray()), is( new StringJoiner(ln) .add(menu) .add("---------------- All Items ------------------") .add("1 | test1") .add("testDesc1") .add("2 | test2") .add("testDesc2") .add("------------------- End ---------------------") .add(menu + ln) .toString() ) ); } }
@Test public void whenFindByIdTestItemThenOutputTestItem() { try (Tracker tracker = new Tracker()) { tracker.init(); Item checked = new Item("test", "testDesc", 10L); checked.setId("1"); tracker.add(checked); Input input = new StubInput(new String[]{"4", checked.getId(), "n", "6"}); new StartUI(input, tracker).init(); assertThat( new String(out.toByteArray()), is( new StringJoiner(ln) .add(menu) .add("---------------- Show item ------------------") .add("Selected item : ") .add("1 | test") .add("testDesc") .add("---------------- Show item end --------------") .add(menu + ln) .toString() ) ); } }
@Test public void whenFindByNameTestThenOutputTestItem() { try (Tracker tracker = new Tracker()) { tracker.init(); Item checked = new Item("test", "testDesc", 10L); checked.setId("1"); tracker.add(checked); Input input = new StubInput(new String[]{"5", "test", "6"}); new StartUI(input, tracker).init(); assertThat( new String(out.toByteArray()), is( new StringJoiner(ln) .add(menu) .add("----------------Find items by name-----------") .add("1 | test") .add("testDesc") .add("------------------- End ---------------------") .add(menu + ln) .toString() ) ); } }