[英]DropWizard JAR Signing Bug
我正在嘗試對我的DropWizard 0.7.0應用程序進行單元測試,就像文檔建議的那樣 :
@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
定義的依賴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'
)
}
這里發生了什么?
src/test/java
下創建一個新的JUnit測試類,使其與上面的JUnit測試完全一樣, 除了將MyApplication
替換為DropWizard應用程序主類的名稱之外,並將MyConfiguration
替換為相應的配置隱含。 注意:我的應用程序是用Groovy編寫的,因此使用了Groovy語法。 您可能需要“ Java驗證”上面的代碼片段才能進行編譯。 編輯:請參閱下面的更新
這不是答案。 請不要對此投票。 在OP確認后將其刪除
因此,我想知道您是否已經親自測試了上述步驟。 我遵循了您所說的步驟,當然創建了我自己的類(由於沒有指定任何東西,所以非常簡單,幾乎沒有實現。而且我也沒有得到想要的結果。我也在使用Maven和Java。我不確定問題是否出在groovy上,因為我不是groovy用戶。
在下面,您將逐步找到我的最小,完整和可驗證的示例(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>
我的配置
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration {}
我的應用程序
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());
}
}
位置資源
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));
}
}
測試結果
因此,我似乎錯誤地制作了我的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工件進行交互。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.