【问题标题】:Building Java Project for WireMock为 WireMock 构建 Java 项目
【发布时间】:2018-05-15 23:01:14
【问题描述】:

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

但我想为 WireMock 创建一个 Java 项目。 WireMock 网站提供 sn-ps 入门。但我在基本项目设置中遇到了一些挑战。

这是我遵循的步骤

  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 中的一些代码 sn-ps 之外,java 中没有任何示例项目可用。 here 提供了一个示例 web 应用程序,它对构建普通的旧 java 线模框架没有多大帮助。

感谢您的支持。

谢谢。

【问题讨论】:

    标签: java mocking stubbing wiremock


    【解决方案1】:

    要消除该错误,请尝试:

    • 通过在你的 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")));
    
            }
        }
    

    【讨论】:

    • 太棒了。谢谢。现在工作。我不明白为什么我们需要明确提及。
    • 我可以在单独的函数/单独编写存根并将其添加到 WireMocker 服务器对象吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2020-08-16
    相关资源
    最近更新 更多