简体   繁体   English

为WireMock构建Java项目

[英]Building Java Project for WireMock

I am using wiremock for mocking the services. 我正在使用Wiremock模拟服务。 With WireMock Standlone jar i am able to run my mock api's by placing the .json files in __files folder. 使用WireMock Standlone jar,我可以通过将.json文件放在__files文件夹中来运行模拟api。

But i would like to create a Java project for WireMock. 但是我想为WireMock创建一个Java项目。 WireMock Website provides snippets to get started. WireMock网站提供了摘要以开始使用。 But i some how face challenges in basic project setup. 但是我在基本项目设置中遇到了一些挑战。

Here are the steps i followed 这是我遵循的步骤

  1. Created a gradle project in intellij 在intellij中创建了gradle项目
  2. Added gradle dependency for WireMock. 为WireMock添加了gradle依赖性。 Here is my build.gradle 这是我的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 3.使用以下代码创建示例类,该代码取自WireMock网站

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 但是,在执行此代码时,我的ide控制台出现错误

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. 除了WireMock中的一些代码片段外,Java中没有任何可用的示例项目。 There is a sample web app provided here which is not much helpful in building plain old java wiremock framework. 提供了一种样本Web应用程序在这里是不是在建立普通Java wiremock框架没什么太大的帮助。

Appreciate your support. 感谢您的支持。

Thank you. 谢谢。

To remove that error try: 要消除该错误,请尝试:

  • to replace testCompile by compile in your build.gradle file 通过在build.gradle文件中进行编译来替换testCompile

    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 在运行之前使用“ gradle build”(或在IDE中执行任务)进行构建

Anyway, it looks like the port you are assigning is being ignored by Wiremock. 无论如何,Wiremock似乎忽略了您分配的端口。 At least in my execution. 至少在我执行时。

Update: I added WireMock.configureFor("localhost", wireMockServer.port()); 更新:我添加了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")));

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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