gpt4 book ai didi

java - Maven Surefire 中的类加载和 fork 异常(org.apache.maven.surefire.testset.TestSetFailedException : java. lang.NullPointerException)

转载 作者:行者123 更新时间:2023-12-01 12:29:01 27 4
gpt4 key购买 nike

我试图通过嵌入式jetty服务器和apache wink REST客户端在我的scim Restful服务器实现上运行集成测试,但未能运行测试。这是我的 EmbeddedServerBase 类

package org.picketlink.test.scim;

import java.net.URL;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;
import org.junit.After;
import org.junit.Before;
import org.picketlink.scim.PicketLinkSCIMApplication;

public class EmbeddedWebServerBase {
protected Server server = null;

@Before
public void setUp() throws Exception {
// Start the Jetty embedded container
server = new Server();

server.setConnectors(getConnectors());

this.establishUserApps();

server.start();
}

@After
public void tearDown() throws Exception {
if (server != null) {
server.stop();
try {
server.destroy();
} catch (Exception e) {
//Don't bother
}
server = null;
}
}

/**
* Return the connectors that need to be configured on the server. Subclasses can create as many connectors as they want
*
* @return
*/
protected Connector[] getConnectors() {
Connector connector = new SocketConnector();
connector.setPort(11080);
return new Connector[] { connector };
}

/**
* Establish the user applications - context, servlets etc
*/
protected void establishUserApps() {
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
if (tcl == null) {
tcl = getClass().getClassLoader();
}

final String WEBAPPDIR = "scim";

final String CONTEXTPATH = "/*";

// for localhost:port/admin/index.html and whatever else is in the webapp directory
final URL warUrl = tcl.getResource(WEBAPPDIR);
final String warUrlString = warUrl.toExternalForm();

WebAppContext context = createWebApp(CONTEXTPATH, warUrlString);
context.setClassLoader(getClass().getClassLoader());
context.setExtraClasspath(warUrlString + "/..");

context.setConfigurationClasses(new String[] { "org.eclipse.jetty.webapp.WebInfConfiguration",
"org.eclipse.jetty.webapp.WebXmlConfiguration", "org.eclipse.jetty.webapp.MetaInfConfiguration",
"org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration",
//"org.eclipse.jetty.plus.webapp.PlusConfiguration",
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.webapp.TagLibConfiguration" });

context.setContextPath("/");

ServletHolder servletHolder = new ServletHolder(new HttpServletDispatcher());
servletHolder.setInitParameter("javax.ws.rs.Application", PicketLinkSCIMApplication.class.getName());
context.addServlet(servletHolder, "/*");

server.setHandler(context);
}

protected FilterMapping createFilterMapping(String pathSpec, FilterHolder filterHolder) {
FilterMapping filterMapping = new FilterMapping();
filterMapping.setPathSpec(pathSpec);
filterMapping.setFilterName(filterHolder.getName());
return filterMapping;
}

protected WebAppContext createWebApp(String contextPath, String warURLString) {
WebAppContext webapp = new WebAppContext();
webapp.setContextPath(contextPath);
webapp.setWar(warURLString);

Thread.currentThread().setContextClassLoader(webapp.getClassLoader());
return webapp;
}
}

该异常很可能是在 Thread.currentThread().setContextClassLoader(webapp.getClassLoader()); 中的“webapp.getClassLoader()”处引发的;

这里是 Classloading and Forking problems in Maven Surefire 的链接

这是来自 CrudTest.java 的片段

package org.picketlink.scim.restful.client.test;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.picketlink.scim.core.entities.ObjectFactory;
import org.picketlink.scim.restful.client.Client;
import org.picketlink.scim.restful.client.Tests;
import org.picketlink.test.scim.EmbeddedWebServerBase;

public class CrudTest extends EmbeddedWebServerBase {
private Client client = null;
private ObjectFactory factory = null;

@Before
public void onBefore() {
// create a client to send the user/group crud requests
client = new Client("http://localhost:11080/scim", "matt", "password");

// create an object factory to create the user/group objects
factory = new ObjectFactory();
}

@Test
public void testBasicCrud() {
// test the create user functionality
String gid = Tests.createGroupTest(client, factory);
}
}

这是堆栈跟踪

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.picketlink.scim.restful.client.test.CrudTest
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.432 sec - in org.picketlink.scim.restful.client.test.CrudTest

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.954s
[INFO] Finished at: Fri Sep 26 00:22:46 IST 2014
[INFO] Final Memory: 28M/299M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test (default-test) on project picketlink-rest: ExecutionException: java.lang.RuntimeException: There was an error in the forked process
[ERROR] org.apache.maven.surefire.testset.TestSetFailedException: java.lang.NullPointerException
[ERROR] at org.apache.maven.surefire.common.junit4.JUnit4RunListener.rethrowAnyTestMechanismFailures(JUnit4RunListener.java:206)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:129)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] Caused by: java.lang.NullPointerException
[ERROR] at org.apache.maven.surefire.report.SmartStackTraceParser.getClass(SmartStackTraceParser.java:67)
[ERROR] at org.apache.maven.surefire.report.SmartStackTraceParser.<init>(SmartStackTraceParser.java:57)
[ERROR] at org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter.smartTrimmedStackTrace(JUnit4StackTraceWriter.java:77)
[ERROR] at org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:328)
[ERROR] at org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:312)
[ERROR] at org.apache.maven.surefire.booter.ForkingRunListener.toString(ForkingRunListener.java:258)
[ERROR] at org.apache.maven.surefire.booter.ForkingRunListener.testError(ForkingRunListener.java:132)
[ERROR] at org.apache.maven.surefire.common.junit4.JUnit4RunListener.testFailure(JUnit4RunListener.java:114)
[ERROR] at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java:100)
[ERROR] at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41)
[ERROR] at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:97)
[ERROR] at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:26)
[ERROR] at org.junit.internal.runners.model.EachTestNotifier.addMultipleFailureException(EachTestNotifier.java:33)
[ERROR] at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:24)
[ERROR] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:267)
[ERROR] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
[ERROR] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
[ERROR] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
[ERROR] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
[ERROR] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
[ERROR] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
[ERROR] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
[ERROR] at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
[ERROR] ... 3 more
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

最佳答案

错误的原因对您隐藏。

由于 2 个原因,您的 slf4j 配置导致输出出现 NOP。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

以及你的万无一失的执行 fork 。

你需要做什么。

  1. 关闭您的万无一失的 fork 配置(暂时关闭,直到您知道问题是什么为止)
  2. 修复您的 slf4j 日志记录配置以实际输出某些内容。

一旦您得到一些明确的错误消息,修复您的 jetty 配置应该很容易。

关于java - Maven Surefire 中的类加载和 fork 异常(org.apache.maven.surefire.testset.TestSetFailedException : java. lang.NullPointerException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101900/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com