当运行 JMockIt 并尝试在 testng 中模拟一个从签名 jar 加载的实例时,我得到一个错误(在这种情况下是码头服务器):
FAILED CONFIGURATION: @BeforeClass startServer
java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:943)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:657)
at java.lang.ClassLoader.defineClass(ClassLoader.java:785)
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)
...
pom 中的依赖顺序是正确的,没有模拟,测试运行良好。所以来自Java SecurityException: signer information does not match的提示在这里没有帮助。
有解决方法吗?
这是一个示例测试,应该会产生错误。在这种情况下,我们启动一个完整的容器进行集成测试:
public class MyServletTest {
private final Server server = new Server(PORT);
private MockUp<OpenIDAuthenticationProvider> openIDap;
@BeforeClass
public void startServer() throws Exception {
final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(MyServlet.class, "/my/*");
this.server.setHandler(context);
this.server.start();
this.openIDap = new MockUp<OpenIDAuthenticationProvider>() {
@Mock
void $init(final UserDAO userDao) {}
};
}
@Test
...
}
OpenIDAuthenticationProvider 从 MyServlet 中调用并在启动期间实例化,尽管我不确定这是否重要。
pom.xml 中的相应部分如下所示:
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.0.3.v20130506</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.0.3.v20130506</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>