6

我在尝试在 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

我在另一个项目中使用过 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)));
    }

}

我注意到自 Spring Boot 1.3.0.M5 版本以来,一些测试注释发生了变化,我认为这可能是问题所在。有没有人有同样的问题?

谢谢,

4

3 回答 3

10

使用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>

万一其他人遇到这个问题......

编辑:或者,使用更简洁的依赖项是:

<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>
...
于 2017-04-04T10:06:28.087 回答
9

从wiremock文档

目前建议您使用独立 JAR 作为 Spring Boot 项目的依赖项。这避免了 Jetty 版本的冲突。

于 2017-10-06T12:52:45.427 回答
2

我找到了解决方案。

由于某种原因,Maven 没有导入码头依赖项,所以我不得不声明它们。他们是:

<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>

如果使用 Java 1.8,则可以省略 Jetty 依赖项的版本并使用 Wiremock 2.5.1。但如果它是 Java 1.7,我建议使用 Wiremock 2.4.1 和他的 Jetty 依赖版本,即9.2.13.v20150730

我不知道这是否是最好的解决方案,但对我来说就像一种魅力。希望这可以帮助!

于 2017-02-03T10:42:49.273 回答