0

I am using dropwizard 1.2.4 with log4j 1.2.17. I have followed the instructions as mentioned below

https://github.com/arteam/dropwizard-nologback/

It is throwing the exception like below during unit testing.

java.lang.NoClassDefFoundError: ch/qos/logback/core/filter/Filter

    at io.dropwizard.testing.junit.ResourceTestRule.<clinit>(ResourceTestRule.java:34)
    at com.vnera.restapilayer.api.resources.ApiInfoControllerTest.<clinit>(ApiInfoControllerTest.java:25)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:156)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.core.filter.Filter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 19 more

My test code looks like below

import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import javax.ws.rs.core.Response;

import static org.mockito.Mockito.mock;

@Category(value = UnitTest.class)
public class ApiInfoControllerTest {
    private static ApiNonFunctionalHandler nonFunctionalHandler = mock(ApiNonFunctionalHandler.class);
    private static ApiFilter apiFilter = new ApiFilter(nonFunctionalHandler);
    private static final String authToken = "NetworkInsight xTyAGJmZ8nU8yJDP7LnA8Q==";

    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new ApiInfoController())
            .addProvider(apiFilter).build();

    @Test
    public void testApiVersion() throws Exception {
        Response response = resources.client()
                .target(ApiConstants.INFO_BASE_URL + "/version")
                .request()
                .header("Authorization", authToken)
                .buildGet().invoke();

        Assert.assertNotNull(response);
        Assert.assertEquals(response.toString(), Response.Status.OK.getStatusCode(), response.getStatus());
        final VersionResponse actualError = response.readEntity(VersionResponse.class);
        Assert.assertEquals(actualError.getApiVersion(), ApiConstants.API_VERSION);
    }
}

My main application is working fine. The configuration.yaml for main application looks like below

# Change default server ports
server:
  applicationConnectors:
  - type: http
    port: 8123
  adminConnectors:
  - type: http
    port: 8124
  requestLog:
    type: external
logging:
  type: external

Can someone let me know what could be going wrong and how can I get around this?

EDIT Output of mvn dependency:tree is placed here as I am hitting the character limit here.

tuk
  • 5,941
  • 14
  • 79
  • 162
  • The `pom.xml` of the linked project explicitly excludes `logback-core`. https://github.com/arteam/dropwizard-nologback/blob/master/pom.xml Do you do this, too? –  Apr 29 '18 at 07:42
  • Yes I have excluded. I think that may be the reason for exception as it is looking for `logback` class. I have edited the question and added the output of `mvn dependency:tree` . – tuk Apr 29 '18 at 09:25
  • What happens if you don't exclude `logback-core`? –  Apr 29 '18 at 10:40
  • It will give me some other error due to multiple slf4j bindings as I am using log4j. My goal is to use log4j with dropwizard. – tuk Apr 29 '18 at 11:43

1 Answers1

0

This is a bug in dropwizard 1.2.4 as discussed below

https://github.com/dropwizard/dropwizard/pull/2338

tuk
  • 5,941
  • 14
  • 79
  • 162