Тестирование цикла foreach с помощью jmockit - PullRequest
0 голосов
/ 21 мая 2019

Я пытаюсь написать модульный тест для логики в цикле foreach, но это не удалось из-за исключения «Пропущенные вызовы».

При отладке я вижу, что s3EventNotification.getRecords().size() равно 0.Чем объясняется пропущенная ошибка вызова, но я не могу понять, как заставить ее войти в цикл.Я также попытался добавить в блок Ожидания

s3EventNotificationRecords.size();
                result = 1;

, чтобы получить s3EventNotification.getRecords().size() = 1, но он все еще не входит в цикл.

ниже приведен метод I 'я пытаюсь проверить:

public void receiveMessages(String msg) throws IOException {
        logger.info("Message received to queue. Message = " +msg );
        try {
            ObjectMapper mapper = new ObjectMapper();
            S3EventNotification s3EventNotification = mapper.readValue(msg, S3EventNotification.class);

            s3EventNotification.getRecords().forEach(record -> {
                S3EventNotification.S3Entity s3Entity = record.getS3();

                S3Object s3Object = s3Service.getS3Object(s3Entity.getBucket().getName(), s3Entity.getObject().getKey());
            });
        } catch (Exception e) {
            logger.error("error handling message",e);
            throw e;
        }
    }

и тест, который я написал:

public class DemoAppTest {

    @Tested
    private DemoApp demoApp;

    @Injectable
    private S3Service s3Service;

    @Mocked
    private ObjectMapper mapper;

    @Mocked
    private S3EventNotification s3EventNotification;

    @Mocked
    private S3EventNotification.S3EventNotificationRecord s3EventNotificationRecord;

    @Mocked
    private S3EventNotification.S3Entity s3Entity;

    @Mocked
    private S3Object s3Object;

    @Mocked
    private List<S3EventNotification.S3EventNotificationRecord> s3EventNotificationRecords;

    private String msg ="msg";

    @Test
    public void receiveMessagesTest() throws IOException {
        new Expectations(){
            {
                new ObjectMapper();
                result = mapper;

                mapper.readValue(msg, s3EventNotification.getClass());
                result = s3EventNotification;

                s3EventNotification.getRecords();
                result = s3EventNotificationRecords;

                s3EventNotificationRecord.getS3();
                result = s3Entity;

                s3Service.getS3Object(s3Entity.getBucket().getName(), s3Entity.getObject().getKey());
                result = s3Object;
            }
        };

        demoApp.receiveMessages(msg);
    }
}

Я получил ошибку:

Missing 1 invocation to:
com.amazonaws.services.s3.event.S3EventNotification$S3EventNotificationRecord#getS3()
   on mock instance: com.amazonaws.services.s3.event.S3EventNotification$S3EventNotificationRecord@6c61a903

Я буду очень признателен за любуюПомогите.Спасибо!

...