Spock temporary directories in data-driven test fail after first case

  Kiến thức lập trình

I am using Spock to test some code (Spring app) that involved saving data to disk. I need to test more than one case, so I am using where: keyword to plan multiple test cases. The tested code saves some data into tempDir, then moves that into dataDir.

Somehow, no matter how I try to configure the temporary directories, either by @TempDir or Files.createTempDirectory(), the first test passes fine, and the second fails, as the temporary Path is null. If I comment out any of the cases, the other one passes.

It might be important to note that the tempDir seems fine, the test always fails due to the dataSavedDir being null.

My test spec:

class SimulationStorageSpec extends Specification implements StorageDtos, AsyncTestUtils {

    SimulationStorageService service
    @TempDir Path tempDir
    @TempDir Path dataDir
    @Shared String simulationId = "ABC123"

    def setup() {
        service = new SimulationStorageConfiguration().simulationDataStorageService(tempDir.toString(), dataDir.toString())
    }

    def "should accept raw simulation data file and save it"() {
        when:
        service.processAttachment(datafile, dto)
        then:
        Path dataSavedDir = dataDir.resolve("simulations/${simulationId}/${directoryName}")
        awaitUntil(!isEmpty(tempDir), "File exists in a tempDir")
                .andThen(!isEmpty(dataSavedDir), "File exists in an appropriate directory of a simulation")
                .andThen(isEmpty(tempDir), "tempDir is empty")
        and:
        try (InputStream is = Files.newInputStream(dataSavedDir.toFile().listFiles()[0].toPath())) {
            assert DigestUtils.sha256Hex(is) == dto.getFileHash()
        }
        where:
        datafile                    | dto                                   | directoryName
        singleRawPlotData()         | singleRawPlotDto(simulationId)        | "plots"
        singleRawGeometryData()     | singleRawGeometryDto(simulationId)    | "geometry"
    }

AsyncTestUtils (just a trait to chain PollingConditions, the tested code is asynchronous):

trait AsyncTestUtils <T extends AsyncTestUtils<T>> {
    PollingConditions pollingConditions = new PollingConditions()

    T awaitUntil(Boolean condition, String failMessage = "Assertion failed in awaitUntil") {
        pollingConditions.within(10) {
            assert condition : failMessage
        }
        return (T) this
    }

    T andThen(Boolean condition, String failMessage = "Assertion failed in andThen") {
        pollingConditions.within(10) {
            assert condition : failMessage
        }
        return (T) this
    }
}

Could this be a problem with temporary directory config, my async testing code or something else?

LEAVE A COMMENT