znsio / specmatic

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
https://specmatic.io
MIT License
281 stars 52 forks source link

The parameter givenConfigFileName cannot be passed through the public functions createStub(...) #1375

Open crcaguilerapo opened 4 weeks ago

crcaguilerapo commented 4 weeks ago

https://github.com/znsio/specmatic/blob/1e9c23671e5255e60b42d51210fe7f144847008e/core/src/main/kotlin/io/specmatic/stub/api.kt#L127

I think it's important to be able to pass the Specmatic configuration path for cases when it is stored in a different location than the root of the Java project.

At the moment, that attribute is inaccessible to developers.

pranavgawri commented 1 week ago

Hi @crcaguilerapo ,

Apologies for the late response. Thanks for sharing your concern. I will check this and get back to you ASAP.

Thanks, Pranav

pranavgawri commented 6 days ago

Hi @crcaguilerapo ,

Thanks for your patience. There is an existing supported way to specify the configuration file path when working programmatically.

While the givenConfigFileName parameter is internal, you can specify the configuration file path using the CONFIG_FILE_PATH environment variable. This gives you the flexibility to store your Specmatic configuration file anywhere in your project structure.

Please refer the documentation for more details

Please let me know if this help.

Thanks, Pranav

crcaguilerapo commented 4 days ago

Hello @pranavgawri,

Thank you! You are right, this worked very well for me in the contract tests,

// .../adapter/in/ExampleContractTest.java
@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ExampleContractTest implements SpecmaticContractTest {

    @Inject
    EmbeddedServer server;

    @BeforeAll
    public void setUp() {
        if (!server.isRunning()) {
            server.start();
        }

        System.setProperty("CONFIG_FILE_PATH", "/path/to/specmatic.yaml");
        System.setProperty("host", server.getHost());
        System.setProperty("port", String.valueOf(server.getPort()));
    }

    @Test
    public void initExecution() {}
}

but when setting up the virtual server of Specmatic to perform integration tests, it didn't work.

@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ExampleIntegrationTest {

    private ContractStub stub;

    @BeforeAll
    public void setUp() {
        ...
        System.setProperty("CONFIG_FILE_PATH", "/path/to/specmatic.yaml")
        stub = createStub(HOST, PORT, true);
    }

    @AfterAll
    public void tearDown() throws IOException {
        stub.close();
    }
}

Do you know what could be wrong?

Thank you, Cristian Aguilera