为 WireMock 构建 Java 个项目
Building Java Project for WireMock
我正在使用 wiremock 来模拟服务。使用 WireMock Standlone jar,我可以通过将 .json 文件放在 __files 文件夹中来 [=51=] 我的模拟 api。
但我想为 WireMock 创建一个 Java 项目。 WireMock Website provides 片段开始。但我有些如何在基本项目设置中面临挑战。
这是我遵循的步骤
- 在 intellij 中创建了一个 gradle 项目
添加了 gradle 对 WireMock 的依赖。这是我的 build.gradle
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "com.github.tomakehurst:wiremock:2.17.0"
}
3.Created 使用以下代码的示例 class,从 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
不确定为什么会出现此错误。 java 除了 WireMock 中的一些代码片段外,没有任何示例项目可用。有一个示例 Web 应用程序 provided here 这对构建普通的旧 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")));
}
}
我正在使用 wiremock 来模拟服务。使用 WireMock Standlone jar,我可以通过将 .json 文件放在 __files 文件夹中来 [=51=] 我的模拟 api。
但我想为 WireMock 创建一个 Java 项目。 WireMock Website provides 片段开始。但我有些如何在基本项目设置中面临挑战。
这是我遵循的步骤
- 在 intellij 中创建了一个 gradle 项目
添加了 gradle 对 WireMock 的依赖。这是我的 build.gradle
dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' testCompile "com.github.tomakehurst:wiremock:2.17.0" }
3.Created 使用以下代码的示例 class,从 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
不确定为什么会出现此错误。 java 除了 WireMock 中的一些代码片段外,没有任何示例项目可用。有一个示例 Web 应用程序 provided here 这对构建普通的旧 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")));
}
}