Спок тестирование - переменные получают нулевое значение от нового оператора - PullRequest
0 голосов
/ 01 мая 2019

Ниже мой тестовый код,

def 'test write then update with commit after all operations parallely'() {
    given:
    def outputPath = "writeThenUpdateWithCommitAfterAllOperationsParallely.csv"
    csvManipulator = new CsvManipulator(RESOURCE_PATH, outputPath, FIELD_COUNT, 0
            , new ResourceReaderProvider(), new FileWriterProvider())

    when:
    GParsPool.withPool 100, {
        (0..LOOP-1).eachParallel { row ->
            writeThenUpdate(row, false)
        }
    }
    csvManipulator.commit()

    then:
    Reader reader = new FileReader(outputPath)
    def outputRawCsv = IOUtils.toString(reader)
    expectedRawCsv == outputRawCsv

    cleanup:
    reader.close()
    Files.delete(Paths.get(outputPath))
}

Короче говоря, в режиме отладки в каждой строке я видел, что все переменные outputPath, csvManipulator ... и reader (в блоке then) все null .

Следовательно, проверка заканчивается на NullPointerException, происходящем при закрытии нулевого считывателя.

И вот как это выглядит в режиме отладки: (вы можете видеть, что все переменные равны нулю)

intellij

Что происходит?

1 Ответ

2 голосов
/ 02 мая 2019

Согласно документации Спока: «Блоки« когда »и« тогда »всегда происходят вместе. Они описывают стимул и ожидаемый ответ. Если блоки могут содержать произвольный код , , тогда блоки ограничиваются условия, условия исключения, взаимодействия и определения переменных . Метод пространственных объектов может содержать несколько пар блоков «когда - то». " Спок блоки

Эти две строки:

Reader reader = new FileReader(outputPath)
def outputRawCsv = IOUtils.toString(reader)

Необходимо указывать в вышеприведенном блоке, как показано ниже:

def 'test write then update with commit after all operations parallely'() {
    given:
    def outputPath = "writeThenUpdateWithCommitAfterAllOperationsParallely.csv"
    csvManipulator = new CsvManipulator(RESOURCE_PATH, outputPath, FIELD_COUNT, 0
            , new ResourceReaderProvider(), new FileWriterProvider())

    when:
    GParsPool.withPool 100, {
        (0..LOOP-1).eachParallel { row ->
            writeThenUpdate(row, false)
        }
    }
    csvManipulator.commit()
    Reader reader = new FileReader(outputPath)
    def outputRawCsv = IOUtils.toString(reader)

    then:
    expectedRawCsv == outputRawCsv

    cleanup:
    reader.close()
    Files.delete(Paths.get(outputPath))
}

Я бы также рассмотрел чтение файла, используя метод, предложенный Леонардом:

def outputRawCsv = new File(outputPath).text

Добавление всего в:

def 'test write then update with commit after all operations parallely'() {
    given:
    def outputPath = "writeThenUpdateWithCommitAfterAllOperationsParallely.csv"
    csvManipulator = new CsvManipulator(RESOURCE_PATH, outputPath, FIELD_COUNT, 0
            , new ResourceReaderProvider(), new FileWriterProvider())

    when:
    GParsPool.withPool 100, {
        (0..LOOP-1).eachParallel { row ->
            writeThenUpdate(row, false)
        }
    }
    csvManipulator.commit()
    def outputRawCsv = new File(outputPath).text

    then:
    expectedRawCsv == outputRawCsv

    cleanup:
    reader.close()
    Files.delete(Paths.get(outputPath))
}

Если это не сработает, понадобится http://stackoverflow.com/help/mcve

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...