繁体   English   中英

为WireMock构建Java项目

[英]Building Java Project for WireMock

我正在使用Wiremock模拟服务。 使用WireMock Standlone jar,我可以通过将.json文件放在__files文件夹中来运行模拟api。

但是我想为WireMock创建一个Java项目。 WireMock网站提供了摘要以开始使用。 但是我在基本项目设置中遇到了一些挑战。

这是我遵循的步骤

  1. 在intellij中创建了gradle项目
  2. 为WireMock添加了gradle依赖性。 这是我的build.gradle

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

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

但是,在执行此代码时,我的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

不知道为什么会发生此错误。 除了WireMock中的一些代码片段外,Java中没有任何可用的示例项目。 提供了一种样本Web应用程序在这里是不是在建立普通Java wiremock框架没什么太大的帮助。

感谢您的支持。

谢谢。

要消除该错误,请尝试:

  • 通过在build.gradle文件中进行编译来替换testCompile

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

  • 在运行之前使用“ gradle build”(或在IDE中执行任务)进行构建

无论如何,Wiremock似乎忽略了您分配的端口。 至少在我执行时。

更新:我添加了WireMock.configureFor("localhost", wireMockServer.port()); 在启动服务器的行之后,它起作用了!

    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