首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Struts2-Junit-Plugin执行任何StrutsTestCase,操作方法为空。

无法使用Struts2-Junit-Plugin执行任何StrutsTestCase,操作方法为空。
EN

Stack Overflow用户
提问于 2019-01-15 17:34:04
回答 1查看 645关注 0票数 0

首先,对不起,如果我解释得不够清楚,我会尽力做到最好。最近,我从C/C#转向了Java,我有点不知所措。

我正在开发一个使用Struts2+Hibernate的项目(我不使用Spring);我的想法是使用struts2-JUnit插件来测试这些操作。

我遵循了strut2-jun-plugin教程,但没有成功,经过大量的研究和尝试,所有可能的解决方案张贴到任何地方,我都找不到好的解决方案。

“问题”(因为我不确定这是否是个问题.)当我尝试运行StrutsTestCase时,在setUp期间,我就是这样做的:

代码语言:javascript
复制
super.setUp();
initServletMockObjects();
setupBeforeInitDispatcher();
dispatcher = initDispatcher(dispatcherInitParams);
setupAfterInitDispatcher(dispatcher);

调用dispatcher = initDispatcher( dispatcherInitParams )时,dispatcherInitParams为null,并引发以下堆栈跟踪:

代码语言:javascript
复制
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR CdiObjectFactory [findBeanManager]: Could not find BeanManager instance for any given JNDI key, giving up
ERROR CdiObjectFactory Struts2 CDI integration could not be initialized.
ERROR DefaultConversionPropertiesProcessor Conversion registration error
 java.lang.NullPointerException
    at org.apache.struts2.cdi.CdiObjectFactory.getInjectionTarget(CdiObjectFactory.java:175)
    at org.apache.struts2.cdi.CdiObjectFactory.buildBean(CdiObjectFactory.java:148)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:177)
    at com.opensymphony.xwork2.conversion.impl.DefaultTypeConverterCreator.createTypeConverter(DefaultTypeConverterCreator.java:40)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.loadConversionProperties(DefaultConversionPropertiesProcessor.java:86)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.processRequired(DefaultConversionPropertiesProcessor.java:68)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.init(DefaultConversionPropertiesProcessor.java:59)
    at com.opensymphony.xwork2.inject.InitializableFactory.create(InitializableFactory.java:45)
    at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
    at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:118)
    at com.opensymphony.xwork2.inject.ContainerBuilder$8.call(ContainerBuilder.java:626)
    at com.opensymphony.xwork2.inject.ContainerBuilder$8.call(ContainerBuilder.java:623)
    at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:555)
    at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:623)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:187)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(StrutsTestCaseHelper.java:44)
    at org.apache.struts2.StrutsTestCase.initDispatcher(StrutsTestCase.java:236)
    at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:216)
    at com.xxxx.xxxxx.xx.xxx.PasswordActionTest.setUp(PasswordActionTest.java:31)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    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:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
ERROR DefaultConversionPropertiesProcessor Conversion registration error
 java.lang.NullPointerException
    at org.apache.struts2.cdi.CdiObjectFactory.getInjectionTarget(CdiObjectFactory.java:175)
    at org.apache.struts2.cdi.CdiObjectFactory.buildBean(CdiObjectFactory.java:148)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:177)
    at com.opensymphony.xwork2.conversion.impl.DefaultTypeConverterCreator.createTypeConverter(DefaultTypeConverterCreator.java:40)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.loadConversionProperties(DefaultConversionPropertiesProcessor.java:86)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.process(DefaultConversionPropertiesProcessor.java:64)
    at com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor.init(DefaultConversionPropertiesProcessor.java:60)
    at com.opensymphony.xwork2.inject.InitializableFactory.create(InitializableFactory.java:45)
    at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
    at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:118)
    at com.opensymphony.xwork2.inject.ContainerBuilder$8.call(ContainerBuilder.java:626)
    at com.opensymphony.xwork2.inject.ContainerBuilder$8.call(ContainerBuilder.java:623)
    at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:555)
    at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:623)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:187)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(StrutsTestCaseHelper.java:44)
    at org.apache.struts2.StrutsTestCase.initDispatcher(StrutsTestCase.java:236)
    at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:216)
    at com.xxx.xxxx.xxx.xxxx.PasswordActionTest.setUp(PasswordActionTest.java:31)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    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:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

在这之后,即使我得到了错误,我也可以继续测试,例如,以下测试:

代码语言:javascript
复制
@Test
public void testGetActionProxy() throws Exception {

    //set parameters before calling getActionProxy
    PasswordUpdate pu = new PasswordUpdate();
    pu.setCurrentPassword("current");
    pu.setNewPassword("new");
    pu.setNewPasswordConfirm("new");
    request.setParameter("passwordUpdate.currentPassword", pu.getCurrentPassword());
    request.setParameter("passwordUpdate.newPassword", pu.getNewPassword());
    request.setParameter("passwordUpdate.newPasswordConfirm", pu.getNewPasswordConfirm());

    ActionProxy proxy = getActionProxy("/UpdatePassword.action");
    assertNotNull(proxy);

    PasswordAction action = (PasswordAction) proxy.getAction();
    assertNotNull(action);

    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
    assertNotNull(action.getPasswordUpdate());
    assertEquals("current", action.getPasswordUpdate().getCurrentPassword());
    assertEquals("new", action.getPasswordUpdate().getNewPassword());
    assertEquals("new", action.getPasswordUpdate().getNewPasswordConfirm());

}

第二个问题是,当调用时,它似乎找不到操作:

ActionProxy proxy = getActionProxy("/UpdatePassword.action");

它在getActionProxy中抛出一个异常:

代码语言:javascript
复制
protected ActionProxy getActionProxy(String uri) {
        request.setRequestURI(uri);
        ActionMapping mapping = getActionMapping(request);
        String namespace = mapping.getNamespace();
        String name = mapping.getName();
        String method = mapping.getMethod();

        Configuration config = configurationManager.getConfiguration();
        ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                namespace, name, method, new HashMap<String, Object>(), true, false);

        initActionContext(proxy.getInvocation().getInvocationContext());

        // this is normally done in onSetUp(), but we are using Struts internal
        // objects (proxy and action invocation)
        // so we have to hack around so it works
        ServletActionContext.setServletContext(servletContext);
        ServletActionContext.setRequest(request);
        ServletActionContext.setResponse(response);

        return proxy;
    }

此时,String方法为null,下面的调用将引发异常:

config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( ActionProxy代理=ActionProxy名称空间、名称、方法、新HashMap()、true、false);

在我放弃之前知道吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-21 13:41:46

如果其他人在这篇文章中出现了同样的问题

代码语言:javascript
复制
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR CdiObjectFactory [findBeanManager]: Could not find BeanManager instance for any given JNDI key, giving up
ERROR CdiObjectFactory Struts2 CDI integration could not be initialized.
ERROR DefaultConversionPropertiesProcessor Conversion registration error

您可以通过从配置支柱中排除2-cdi-plugin来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54204089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档