Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException with Wiremock 2.5.1 and Spring Boot 1.5.1

I'm having problems when trying to configure a test infrastructure in Spring Boot 1.5.1.RELEASE and Wiremock 2.5.1. It's throwing:

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

I`ve used Wiremock 2.4.1 and Spring Boot 1.3.0.M5 in another project and never had this problem.

My Test class:

@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)));
    }

}

I've noticed that some test annotations have changed since version 1.3.0.M5 of Spring Boot and I think this could be the problem. Does Anyone had the same problem?

Thanks,

like image 574
Pimenta Avatar asked Feb 02 '17 16:02

Pimenta


1 Answers

Using spring-boot-dependencies:1.5.2 and spring-cloud-dependencies:Dalston.RC1 I had to add the following to get Wiremock to work:

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

In case anyone else hits this issue...

EDIT: Alternatively, a much more concise dependency to use would be:

<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>
...
like image 191
Donovan Muller Avatar answered Sep 17 '22 19:09

Donovan Muller