DropWizard JAR签名错误

时间:2014-12-14 18:05:56

标签: testing jersey jax-rs dropwizard jar-signing

我正在尝试对我的DropWizard 0.7.0应用进行单元测试,就像docs recommend

@Test
public void runRegisertsLocationResource() {
    given:
    Environment environment = Mockito.mock(Environment)
    JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment) 
    MyApplication fixture = new MyApplication()
    MyConfiguration config = new MyConfiguration()

    Mockito.when(environment.jersey()).thenReturn(jersey)

    when:
    fixture.run(config, environment)

    then:
    Mockito.verify(jersey).register(Mockito.any(LocationResource))
}

当我跑步时,我得到:

java.lang.SecurityException: class "javax.servlet.DispatcherType"'s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(ClassLoader.java:952)
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:666)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at io.dropwizard.Configuration.<init>(Configuration.java:62)
    at io.auditcloud.micros.backend.service.config.BackendConfiguration.<init>(BackendConfiguration.groovy)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:186)
    at io.auditcloud.micros.backend.service.BackendApplicationTest.runRegisertsAllProviders(BackendApplicationTest.groovy:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

以下是build.gradle中定义的依赖关系:

dependencies {
    compile (
        'com.codahale.metrics:metrics-core:3.0.2',
        'com.codahale.metrics:metrics-json:3.0.1',
        'com.wordnik:swagger-jaxrs_2.10:1.3.4',
        'io.dropwizard:dropwizard-core:0.7.0',
        'io.dropwizard:dropwizard-auth:0.7.0',
        'org.apache.commons:commons-lang3:3.3.2',
        'io.dropwizard:dropwizard-client:0.7.0',
        'org.eclipse.jetty:jetty-servlets:8.1.14.v20131031',
        'com.google.inject:guice:3.0'
    )

    testCompile(
        'org.mockito:mockito-all:1.9.0'
    )
}

这里发生了什么?


更新:如何重现(一步一步)

  1. 创建一个新的DropWizard项目并使用上面提到的确切依赖项(我使用Gradle,但任何工作都应该没问题。)
  2. src/test/java下创建一个新的JUnit测试类,并使其看起来与上面的JUnit测试完全相同,除了替换MyApplication之外的任何名称DropWizard应用程序的主类,并用相应的配置impl替换MyConfiguration注意:我的应用程序是用Groovy编写的,因此是Groovy语法;您可能需要在上面的“Java-ify”我的代码片段中进行编译。
  3. 运行测试;你应该得到与我上面发布的相同的例外。

1 个答案:

答案 0 :(得分:0)

修改:请参阅下面的更新

这不是答案。请不要投票。它将在OP

确认后删除

所以我想知道你是否已经为自己测试了上述步骤。我按照你所说的步骤,当然创建了我自己的课程(超级简单,几乎没有实现,因为你还没有指定任何。我不会得到你正在经历的结果我也在使用Maven和Java。无论问题是否与groovy有关,我都不确定,因为我不是一个常规的用户。

您将在下面找到我的Minimal, Complete, and Verifiable example (MCVE)实施步骤。 我建议你创建类似的东西来重现问题。如您所述,从头开始创建项目(可能使用以下示例作为起点),然后开始添加功能,直到它再现问题。这也是自己发现问题根源的好方法。

我将从一个简单的Groovy项目开始,如果一个简单的项目,如下面的那个导致问题,那么它可能是一个Groovy问题,你应该用MCVE编辑你的帖子,以便其他熟悉Groovy的人可以尝试并找到问题。

如果它不是Groovy问题,并且你在我的MCVE之上构建了问题,你就无法找出原因以及如何解决它,然后请发布MCVE。

<强>的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stackoverflow.spring</groupId>
    <artifactId>dropwizard-stackoverflow</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <dropwizard.version>0.7.0</dropwizard.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-auth</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-client</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-core</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.codahale.metrics</groupId>
            <artifactId>metrics-json</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

<强> MyConfiguration

import io.dropwizard.Configuration;

public class MyConfiguration extends Configuration {}

<强>所有MyApplication

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class MyApplication extends Application<MyConfiguration> {
    @Override
    public void initialize(Bootstrap<MyConfiguration> btstrp) {}

    @Override
    public void run(MyConfiguration t, Environment e) throws Exception {
        e.jersey().register(new LocationResource());
    }   
}

<强> LocationResource

import javax.ws.rs.Path;

@Path("/location")
public class LocationResource {}

<强>测试

import dropwizard.stackoverflow.resource.LocationResource;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Environment;
import org.junit.Test;
import org.mockito.Mockito;

public class SimpleDropWizardTest {
    @Test
    public void runRegisertsLocationResource() throws Exception {

        Environment environment = Mockito.mock(Environment.class);
        JerseyEnvironment jersey = Mockito.mock(JerseyEnvironment.class);
        MyApplication fixture = new MyApplication();
        MyConfiguration config = new MyConfiguration();

        Mockito.when(environment.jersey()).thenReturn(jersey);

        fixture.run(config, environment);

        Mockito.verify(jersey).register(Mockito.any(LocationResource.class));
    }
}

测试结果

enter image description here


更新

所以我似乎错误地使我的MCVE。出于某种原因,我错过了

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>8.1.14.v20131031</version>
</dependency>

恰好如此,这似乎是吸烟枪。当我将这个依赖项添加到上面的项目中时,我会得到一个类似的异常,这个异常并不完全相同,但在类加载过程中详细说明了相同的问题。

如果你摆脱了这种依赖关系,它将适用于上面的例子。似乎是版本冲突。 Dropwizard已经依赖于Jetty 9.0.7.v20131107。您可以看到Maven依赖关系树

[INFO] +- io.dropwizard:dropwizard-core:jar:0.7.0:compile
...
[INFO] |  +- io.dropwizard:dropwizard-jetty:jar:0.7.0:compile
[INFO] |  |  +- com.codahale.metrics:metrics-jetty9:jar:3.0.1:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-server:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-io:jar:9.0.7.v20131107:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-servlet:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-security:jar:9.0.7.v20131107:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-servlets:jar:9.0.7.v20131107:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-continuation:jar:9.0.7.v20131107:compile
[INFO] |  |  \- org.eclipse.jetty:jetty-http:jar:9.0.7.v20131107:compile

基本上,当您声明顶级依赖项(与Jetty一样)时,它将取代任何传递依赖项。因此,您有一个Jetty 9.0.7工件尝试与声明的8.1.14 servlet工件进行交互。