Dropwizard测试 - 抛出NoClassDefFoundError的ResourceTestRule:ch / qos / logback / core / filter / Filter

时间:2018-04-27 15:40:43

标签: java-8 dropwizard

我正在使用dropwizard 1.2.4和log4j 1.2.17。我按照下面提到的说明进行了操作

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

在单元测试期间抛出如下所示的异常。

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

我的测试代码如下所示

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

我的主要应用程序运行正常。主应用程序的configuration.yaml如下所示

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

有人可以告诉我可能出现的问题吗?我该如何解决这个问题?

修改  mvn dependency:tree的输出位于here,因为我在此处达到了字符数限制。

1 个答案:

答案 0 :(得分:0)

这是dropwizard 1.2.4中的一个错误,如下所述

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