mercredi 8 juillet 2020

Mockito return different values for a stub method from a list i have in test case

I am getting null pointer exception for this part of mocking a stub method to return from a list . My inputProvider is a mockito spied method. This method testCase is being called by many test methods of similar test cases.

private void testCase(String config, String orders, List<String> outputs){
        try {
            CoffeeMachineRunner machineRunner = new CoffeeMachineRunner();
            when(inputProvider.getConfigInput()).thenReturn(config);
            List<Order> orderList = getOrderFromJson(orders);
            Iterator<Order> it = orderList.iterator();
            when(inputProvider.hasNextInput()).thenAnswer(new Answer<Boolean>() {
                private Iterator<Order> iterator = it;
                @Override
                public Boolean answer(InvocationOnMock invocation) throws Throwable {
                    return iterator.hasNext();
                }
            });
            when(inputProvider.getNextInput()).thenAnswer(new Answer<Order>() {
                private Iterator<Order> iterator = it;
                @Override
                public Order answer(InvocationOnMock invocation) throws Throwable {
                    return iterator.next();
                }
            });
            machineRunner.simulate(inputProvider);
            assertStringIn(outputs, outContent.toString());
        } catch (Exception e) {
            assertTrue(false);
        }
    }

Basically, how do i return a value from this list using a shared iterator over two method calls. hasNext and getNext ?

Aucun commentaire:

Enregistrer un commentaire