Pim*_*nta 4 spring-boot wiremock
尝试在Spring Boot 1.5.1.RELEASE和Wiremock 2.5.1中配置测试基础架构时遇到问题.它在扔:
java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:124)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
at com.github.tomakehurst.wiremock.junit.WireMockRule.<init>(WireMockRule.java:42)
at com.github.tomakehurst.wiremock.junit.WireMockRule.<init>(WireMockRule.java:38)
at br.com.mobicare.minhaoi.WiremockTest.<clinit>(WiremockTest.java:21)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
at java.lang.reflect.Field.get(Field.java:393)
at org.junit.runners.model.FrameworkField.get(FrameworkField.java:73)
at org.junit.runners.model.TestClass.getAnnotatedFieldValues(TestClass.java:230)
at org.junit.runners.ParentRunner.classRules(ParentRunner.java:255)
at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:244)
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:362)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:253)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 28 more
Run Code Online (Sandbox Code Playgroud)
我在另一个项目中使用了Wiremock 2.4.1和Spring Boot 1.3.0.M5,从未遇到过这个问题.
我的测试课:
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class WiremockTest {
@ClassRule
public static WireMockRule wireMockRule = new WireMockRule(options().port(8888).notifier(new ConsoleNotifier(true)));
@Test
public void testStub() {
wireMockRule.stubFor(get(urlEqualTo("/test"))
.willReturn(aResponse()
.withHeader("Content-type", "application/json").withStatus(200)));
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到自Spring Boot的1.3.0.M5版本以来,一些测试注释发生了变化,我认为这可能是问题所在.有没有人有同样的问题?
谢谢,
使用spring-boot-dependencies:1.5.2和spring-cloud-dependencies:Dalston.RC1我必须添加以下内容以使Wiremock工作:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
<dependencies>
...
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
万一其他人遇到这个问题......
编辑:或者,使用更简洁的依赖将是:
<dependencies>
...
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<!-- Don't need all the jetty and httpclient deps. -->
</dependencies>
...
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案。
由于某种原因,Maven 没有导入 jetty 依赖项,所以我必须声明它们。他们是:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果使用Java 1.8,则可以省略Jetty依赖项的版本并使用Wiremock 2.5.1。但如果它是 Java 1.7,就像我的一样,我建议使用 Wiremock 2.4.1 及其 Jetty 依赖项版本9.2.13.v20150730。
我不知道这是否是最好的解决方案,但对我来说就像一个魅力。希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
3934 次 |
| 最近记录: |