1

I am using wiremock for mocking the services. With WireMock Standlone jar i am able to run my mock api's by placing the .json files in __files folder.

But i would like to create a Java project for WireMock. WireMock Website provides snippets to get started. But i some how face challenges in basic project setup.

Here are the steps i followed

  1. Created a gradle project in intellij
  2. Added gradle dependency for WireMock. Here is my build.gradle

    dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile "com.github.tomakehurst:wiremock:2.17.0"
    }
    

3.Created a sample class with following code , taken this code from WireMock website

import com.github.tomakehurst.wiremock.WireMockServer;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static 
     com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

public class samplemock {

public static void main(String args[]){
    WireMockServer wireMockServer = new WireMockServer(options().port(9999));
    wireMockServer.start();
    stubFor(get(urlEqualTo("/test"))
            .willReturn(aResponse()
                    .withBody("Hello")));
}
}

But , on executing this code i am getting errors in my ide console

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Resources
at com.github.tomakehurst.wiremock.core.WireMockConfiguration.<init>(WireMockConfiguration.java:58)
at com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig(WireMockConfiguration.java:104)
at com.github.tomakehurst.wiremock.core.WireMockConfiguration.options(WireMockConfiguration.java:108)
at com.tech.samplemock.main(samplemock.java:11)
 Caused by: java.lang.ClassNotFoundException: com.google.common.io.Resources
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more

Not sure why this error occured. And there is no any sample project available in java except some code snippets in WireMock. There is a sample web app provided here which is not much helpful in building plain old java wiremock framework.

Appreciate your support.

Thank you.

1
  • If you created any standalone project using wiremock, Please share the template, i'm trying to create one. Commented Apr 24, 2023 at 16:13

1 Answer 1

2

To remove that error try:

  • to replace testCompile by compile in your build.gradle file

    dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile "com.github.tomakehurst:wiremock:2.17.0" }

  • to build using 'gradle build' (or executing the tasks in the IDE) before running it

Anyway, it looks like the port you are assigning is being ignored by Wiremock. At least in my execution.

Update: I added WireMock.configureFor("localhost", wireMockServer.port()); after the line that starts the server and it worked!

    import com.github.tomakehurst.wiremock.WireMockServer;
    import com.github.tomakehurst.wiremock.client.WireMock;

    import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
    import static com.github.tomakehurst.wiremock.client.WireMock.get;
    import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
    import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;


    public class SampleMock {

        public static void main(String args[]){

            WireMockServer wireMockServer = new WireMockServer(options().port(9999));
            wireMockServer.start();
            WireMock.configureFor("localhost", wireMockServer.port());

            stubFor(get(urlEqualTo("/test"))
                    .willReturn(aResponse()
                            .withBody("Hello")));

        }
    }
2
  • Great. Thanks. Working now. i dont understand why we need to explicitly mention that. Commented May 16, 2018 at 14:29
  • can i write the stubfor in separate function / in separate and add it to WireMocker server object ? Commented May 16, 2018 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.