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

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

这是我遵循的步骤


在intellij中创建了gradle项目
为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中没有任何可用的示例项目。 here提供了一个示例Web应用程序,它对构建普通的旧Java Wiremock框架没有太大帮助。

感谢您的支持。

谢谢。

最佳答案

要消除该错误,请尝试:


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

dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile "com.github.tomakehurst:wiremock:2.17.0"}
在运行之前使用“渐变构建”(或在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")));

        }
    }

关于java - 为WireMock构建Java项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50360276/

10-11 19:13