使用Wiremock 2.5.1和Spring Boot 1.5.1的ClassNotFoundException

时间:2017-02-02 16:01:04

标签: spring-boot wiremock

尝试在Spring Boot 1.5.1.RELEASE和Wiremock 2.5.1中配置测试基础架构时遇到问题。它在扔:

struct fraction {
  int numerator, denominator;
  bool operator<(const fraction& f) const {
    return numerator*f.denominator < f.numerator*denominator;
  }
}

我在另一个项目中使用了Wiremock 2.4.1和Spring Boot 1.3.0.M5,从未遇到过这个问题。

我的测试课程:

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

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

谢谢,

3 个答案:

答案 0 :(得分:6)

使用spring-boot-dependencies:1.5.2spring-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>
...

答案 1 :(得分:5)

来自电线公司documentation

  

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

答案 2 :(得分:2)

我找到了解决方案。

由于某些原因,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>

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

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