I have method for BillServiceImpl which requires to add your option first before any following methods can be run. Howerver, I don't know how to write the test for this services.
My services is like this:
@Override
public void addBill(Bill bill, Menu menu) {
Scanner scanner = new Scanner(System.in);
List<BillItems> billItemsList = bill.getBillItemsList();
menu();
System.out.print("Insert menu you want to be served: ");
option = scanner.nextLine();
try {
switch (option){
case "1":
menuPrinter.printMenu(menu.getFoodMenu());
foodMenuOption();
System.out.print("Choose menu you want to eat: ");
menuOption = scanner.nextLine();
switch (menuOption) {
case "1" -> {
BreakfastMenu breakfastMenu = menu.getFoodMenu().getBreakfastMenu();
if(breakfastMenu == null){
throw new NullPointerException("Breakfast menu is blank !!!");
}
billItemsList = billItemServices.addMenuItemsToBill(billItemsList, breakfastMenu);
bill.setBillItemsList(billItemsList);
}
case "2" -> {
LunchMenu lunchMenu = menu.getFoodMenu().getLunchMenu();
if(lunchMenu == null){
throw new NullPointerException("Lunch menu is blank !!!");
}
billItemsList = billItemServices.addMenuItemsToBill(billItemsList, lunchMenu);
bill.setBillItemsList(billItemsList);
}
case "3" -> {
DinnerMenu dinnerMenu = menu.getFoodMenu().getDinnerMenu();
if(dinnerMenu == null){
throw new NullPointerException("Dinner menu is blank !!!");
}
billItemsList = billItemServices.addMenuItemsToBill(billItemsList, dinnerMenu);
bill.setBillItemsList(billItemsList);
}
default -> System.out.println("No option found !!!");
}
break;
case "2":
menuPrinter.printMenu(menu.getDrinkMenu());
drinkMenuOption();
System.out.print("Choose menu you want to drink: ");
menuOption = scanner.nextLine();
switch (menuOption) {
case "1" -> {
Alcohol alcohol = menu.getDrinkMenu().getAlcohol();
if(alcohol == null){
throw new NullPointerException("Alcohol menu is blank !!!");
}
billItemsList = billItemServices.addMenuItemsToBill(billItemsList, alcohol);
bill.setBillItemsList(billItemsList);
}
case "2" -> {
SoftDrinks softDrinks = menu.getDrinkMenu().getSoftDrinks();
if(softDrinks == null){
throw new NullPointerException("Soft drink menu is blank !!!");
}
billItemsList = billItemServices.addMenuItemsToBill(billItemsList, softDrinks);
bill.setBillItemsList(billItemsList);
}
default -> System.out.println("No option found !!!");
}
break;
case "e":
break;
default:
System.out.println("No option found !!!");
break;
}
}catch (NullPointerException exception){
System.out.println(exception.getMessage());
}
}
My test is like this:
@RunWith(MockitoJUnitRunner.class)
public class BillServicesTest {
private BillServices billServices;
@Before
public void init(){
billServices = new BillServicesImpl();
}
@Test
public void whenAddNewMenuItemToBill_returnNewBill(){
Bill bill = new Bill();
Menu menu = new Menu();
billServices.addBill(bill,menu);
verify(billServices).addBill(bill,menu);
}
However, when I run the test, it keeps loading state infinetely. It could be that the method requires the informtion from scanner for switch function.
How should I write the test for this method? I am beginner for testing, thanks for your support.