bmuschko / gradle-tomcat-plugin

Gradle plugin supporting deployment of your web application to an embedded Tomcat web container
Apache License 2.0
531 stars 126 forks source link

Want to use embedded tomcat for Pact Test #167

Closed manishs85 closed 6 years ago

manishs85 commented 6 years ago

I have included the plugin using the following :

dependencies { classpath ("com.bmuschko:gradle-tomcat-plugin:2.4.2") }

apply plugin: "com.bmuschko.tomcat"

dependencies { def tomcatVersion = '8.0.42' tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}" }

Can I use the embedded tomcat to run my pact tests? When I try to run pactVerify I am just getting the following but no tests are executed:

Tomcat server started The Server is running at http://localhost:8080/

but when I hit localhost:8080 on the browser, it gives 404.

bmuschko commented 6 years ago

What exactly are Pact tests? I'd need more context e.g. where does the task pactVerify come from. Do you have a complete example?

manishs85 commented 6 years ago

A pact test is a kind of specific Integration test used for defining contracts between the consumer and producer. (https://github.com/DiUS/pact-jvm)

I am using the following plugin : https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-gradle which introduces a pactVerify Task. When running this task it has to start the application on localhost at a random available port and start running the pact files from a given location (in my case these are JSON files which define the request and response format). The pact verification is to assert that when the given request format is triggered, the response coming back is as per the format defined in the pact.

I have defined my serviceProviders as below:

pact {

serviceProviders {

    // You can define as many as you need, but each must have a unique name
    provider1 {
        // All the provider properties are optional, and have sensible defaults (shown below)
        protocol = 'http'
        host = 'localhost'
        path = '/'

        **startProviderTask = tomcatRun** //I am giving the startProviderTask tomcatRun to start the embedded tomcat. Earlier I was using a custom task here which was running the bootRun command.
        terminateProviderTask = tomcatStop
        **port = tomcat.httpPort** //I need to get the random port on which the embedded tomcat started. Earlier I was using a custom task to read the port from the application logs

        // Again, you can define as many consumers for each provider as you need, but each must have a unique name
        hasPactWith('consumer1') {

            // currently supports a file path using file() or a URL using url()
            pactSource = file('path/to/provider1-consumer1-pact.json')

        }

        // Or if you have many pact files in a directory
        hasPactsWith('manyConsumers') {

            // Will define a consumer for each pact file in the directory.
            // Consumer name is read from contents of pact file
            pactFileLocation = file('path/to/pacts')

        }
    }

}
bmuschko commented 6 years ago

Generally speaking this plugin should work. You can form a lifecycle (start Tomcat, run tests, stop Tomcat) with the help of task dependencies (dependsOn, finalizedBy).

It seems like that Pact Gradle plugin already has some sort of integration with Tomcat so I am not quite sure what they are doing exactly. Can you please ask over there?