Question

I am attempting to use WireMock (v1.32) to mock web services in a maven project. To run the tests, I am using jetty (v8.1.8) to deploy the wars. I've tried the following approaches to run WireMock on jetty (under the jetty plugin in my pom file):

<execution>
                    <id>start-wiremock</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy-war</goal>
                    </goals>
                    <configuration>
                        <war>${project.build.directory}/dependent-war/wiremock-war.war</war>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <webApp>
                            <contextPath>/wiremock</contextPath>
                            <tempDirectory>${project.build.directory}/tmp2</tempDirectory>
                        </webApp>
                        <connectors>
                            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                <port>${wiremock.port}</port>
                            </connector>
                        </connectors>
                        <systemProperties>
                            <systemProperty>
                                <name>shared.system.config.file</name>
                                <value>file:///${project.basedir}/src/test/resources/shared.properties</value>
                            </systemProperty>
                            <systemProperty>
                                <name>file.location.base.path</name>
                                <value>${basedir}</value>
                            </systemProperty>
                        </systemProperties>
                    </configuration>
                </execution>

And:

 <contextHandlers>
                    <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                        <war>${project.build.directory}\dependent-war\wiremock-war.war</war>
                        <tempDirectory>${project.build.directory}/tmp2</tempDirectory>
                        <contextPath>/wiremock</contextPath>
                    </contextHandler> 
                </contextHandlers>

However, I have been unable find any further information on how to run the services on WireMock through the servlet.

I can't find information on how to specify the port and host for WireMock when running on a servlet. Any help on getting this set up would be greatly appreciated.

I'm a complete beginner at using WireMock so if there are any details missing, please ask and I will add them.

Était-ce utile?

La solution

Firstly, I'd strongly suggest that you consider either starting WireMock programmatically, or running the standalone JAR e.g.

java -jar wiremock-1.32-standalone.jar --port <your port number>

At the start of your test code you'll need to tell the client where the server is:

WireMock.configureFor("<wiremock server host>", <your port number>);

There are a couple of options for programmatic startup: 1) If you're using JUnit, you can just add the @Rule as described here: http://wiremock.org/getting-started.html#junit-4-x 2) For any other test framework, you can new up the server: http://wiremock.org/getting-started.html#non-junit-and-general-java-usage

It's worth noting that there are some features of WireMock that aren't available when deployed into a container (mostly fault injection related).

If you have no choice than to run inside a container, then a few things worth noting: The host and port of the server are set in the container settings, so from within the Maven plugin settings you'd want something like this:

<connectors>
    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
       <port>9090</port>
    </connector>
</connectors>

If you've deployed the WAR under a non-root URL, you also need to tell the client about this e.g.

WireMock.configureFor("my.jetty.host", 9090, "/wiremock");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top