Running Wiremock within Tomcat

1,803 views
Skip to first unread message

LazyTesting

unread,
Mar 26, 2018, 11:28:39 AM3/26/18
to wiremock-user
Hi,

I'm trying to get Wiremock running within a Tomcat server as a WAR. I found this example project: https://github.com/tomakehurst/wiremock/blob/master/sample-war/src/main/webappCustomMapping/WEB-INF/web.xml
But I would like to configure my stubs from code instead of using the json config. How could I achieve that?

Thanks in advance,

Maarten

Tom Akehurst

unread,
Mar 26, 2018, 12:07:53 PM3/26/18
to wiremock-user
The WireMock class can act as a client to a remote server, so after you've got WireMock server running in Tomcat, in your test code set up a client instance like this:

WireMock wireMock = new WireMock(<hostname of the Tomcat server>, <your port>, <servlet context path>);

e.g.
WireMock wireMock = new WireMock("your-host.com", 8080, "/my-wiremock-instance");

Then you can start stubbing using this:

wireMock.stubFor(get("/stuff")...

LazyTesting

unread,
Mar 27, 2018, 3:27:42 AM3/27/18
to wiremock-user
Hi Tom,

thanks for your quick reply, I got that working. 
Only there seems to be a bug with the port number.

When I run this:

WireMock wireMock = new WireMock("localhost", 6080, "/my-mock-service");
wireMock
.stubFor(get("/some/thing")
                     
.willReturn(aResponse().withStatus(200)));


The port number is set (correctly) to 6080 on the first line. However on the second line it's reset back to the default 8080 port.
It seems that the givenThat method is creating a new instance of the WireMock client.

LazyTesting

unread,
Mar 27, 2018, 3:31:42 AM3/27/18
to wiremock-user
small addition: using the static configureFor and stubFor methods it's working.

Tom Akehurst

unread,
Mar 27, 2018, 3:59:35 AM3/27/18
to wiremock-user
Yeah, the answer I gave was just for the instance based approach. The static dsl has to be configured separately if you want to use it.

coosm...@web.de

unread,
Jul 2, 2018, 5:43:20 AM7/2/18
to wiremock-user
Hey All, hey Tom,

I'm also trying to run Wiremock within a Tomcat server. But I'm not able to. I tried to use the given Example, but didn't get it running
Also I can't start Wiremock in my own Web-Projekt. 

Here is what I'm trying:

@WebServlet(description = "My First Servlet", urlPatterns = { "/startWM"})
public class startWireMock extends HttpServlet {


       
public startWireMock() {
       
super();
       
       
WireMock.configureFor("localhost", 8080, "/startWM");

       
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/some/thing"))
                 
.willReturn(WireMock.aResponse()
                     
.withHeader("Content-Type", "text/plain")
                     
.withBody("Hello world!")));


     
}
}


If I instantiate startWireMock in my Tomcat-Server by calling "localhost:8080/myProject/startWM" a Exception is raised and I don't get why. Could it be that my concept is wrong? I'm new to Tomcat and Wiremock and the hole Java world, maybe i missed some important detail.

Some part of the Stacktrace:

javax.servlet.ServletException: Error instantiating servlet class startWireMock
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Thread.java:745)

root cause

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: <html><head><title>Apache Tomcat/7.0.56 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /startWM/__admin/mappings</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/startWM/__admin/mappings</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.56</h3></body></html>; line: 1, column: 2]
	com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702)
	com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558)
	com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:456)
	com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1906)
	com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:749)
	com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3850)
	com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
	com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
	com.github.tomakehurst.wiremock.common.Json.read(Json.java:50)
	com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:449)
	com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:417)
	com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:133)
	com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:292)
	com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:287)
	com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:106)
	com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:110)
	startWireMock.<init>(startWireMock.java:36)

Thanks in advance,

Jakob
Reply all
Reply to author
Forward
0 new messages