Spring Cloud Contract Producer Flow: FileNotFoundException is thrown when running generated ContractsTest

64 views Asked by At

The ContractsTest and corresponding yml file is beeing generated correctly in the same folder. I'am getting a FileNotFoundException when running the test though.

testClass.getClass().getResource(relativePath) in fileToBytes Method is returning null value.

stacktrace:

java.lang.IllegalStateException: java.io.FileNotFoundException: test_status_tracking_update.yml

at org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.fileToBytes(ContractVerifierUtil.java:83)
at org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.contract(ContractVerifierUtil.java:136)
at de.test.bla.test.cdc.base.ContractsTest.validate_test_status_tracking_update(ContractsTest.java:26)
1

There are 1 answers

0
rasilva On

It seems that the location of the file is relative to the contracts: make sure the test file is at the same folder level as the contracts (when I try to put the file elsewhere, it was not resolved).

I have a structure like this:

src >> test >> resources >> contracts >> users >> test.kts

src >> test >> resources >> contracts >> users >> file.json

And a test like this:

package contracts.users

import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract


contract {
    description = "should return 200 and response"
    request {
        method = GET
        url = url("/users/123")
        headers {
            contentType = APPLICATION_JSON
        }
    }
    response {
        status = OK
        headers {
            contentType = APPLICATION_JSON
        }
        body = bodyFromFile("file.json")
    }
}

And it works fine