简体   繁体   中英

Building Java Project for WireMock

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.

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")));

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM