Struts 2.5运行Junit 4测试时出现查找Struts插件父包时的运行时错误。

huangapple go评论78阅读模式
英文:

Struts 2.5 running Junit 4 test and getting runtime errors finding a Struts plugin parent package

问题

struts版本:2.5.25 - 使用REST插件
Junit 4.12

我正在尝试设置单元测试框架,以开始测试struts的动作,并且在运行基本测试时遇到了问题。当前它正在抱怨找不到父包“jinjava”(这是一个自定义的struts插件,在单元测试之外构建和运行都很好)。

是否需要配置某些内容以使struts配置加载struts插件?以下是我的当前设置,以及在运行测试时看到的错误。

堆栈跟踪:

com.opensymphony.xwork2.config.ConfigurationException: 无法加载配置。
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
    ...
Caused by: com.opensymphony.xwork2.config.ConfigurationException: 未定义父包:jinjava
    ...

单元测试(通过注释配置了Spring Beans,我们没有使用任何XML):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LoginActionSpringConfig.class)
public class LoginActionTest extends IwsStrutsSpringTestCase<LoginAction> {
    
    @Test
    public void getActionMapping() {
        ActionMapping mapping = getActionMapping("/security/login");
        Assert.assertNotNull(mapping);
        Assert.assertEquals("/security", mapping.getNamespace());
        Assert.assertEquals("login", mapping.getName());
    }
}

以下是扩展了StrutsSpringJUnit4TestCase的IwsStrutsSpringTestCase超类。我已经提供了getConfigPath(),包括尝试将struts-plugin.xml放入其中。

public abstract class IwsStrutsSpringTestCase<T> extends StrutsSpringJUnit4TestCase<T> {
    
    @Override
    protected String getConfigPath() {
        return "struts.xml, struts-plugin.xml";
    }
}

以下是在堆栈跟踪中抱怨的jinjava插件的struts-plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.jinjava.basepath" value="WEB-INF/jinJava" />
    <package name="jinjava" extends="struts-default">
        <result-types>
            <result-type name="jinjava" class="com.hs.struts.result.JinJavaResult" />
        </result-types>
    </package>
</struts>

更新于10/18/2020
pom.xml 我还想指出,webapp目录不遵循默认的Maven布局。Web文件存储在/web下。

更新于11/1/2020
以下是我请求的LoginActionSpringConfig.class:

@Configuration
public class LoginActionSpringConfig {
    
    @Bean
    public IIwsAuthenticationService authenticationService() {
        return new IwsAuthenticationServiceTest();
    }
    
    @Bean
    public ISecurityLogDao securityLogDao() {
        return new SecurityLogDaoTest();
    }
    
    @Bean
    public IUsersDepartmentDao usersDepartmentDao() {
        return new UsersDepartmentDaoTest();
    }
    
    @Bean
    public IDepartmentDao departmentDao() {
        return new DepartmentDaoTest();
    }
    
    @Bean
    public IwsCookieInterface iwsCookie() {
        return new IwsCookieTest();
    }
}
英文:

struts version: 2.5.25 - using the REST plugin
Junit 4.12

I am trying to setup the unit testing framework to begin testing struts actions and having issues getting a basic test to run. It is currently complaining that it cannot find the parent package "jinjava" (this is a custom struts plugin which builds and runs fine outside of the unit test)

Is there anything that needs to be configured for the struts configuration to load the struts plugins? Below is my current setup, along with the error I am seeing when the test is run.

Stack Trace:

com.opensymphony.xwork2.config.ConfigurationException: Unable to load configuration.
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
	at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:970)
	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.StrutsJUnit4TestCase.initDispatcher(StrutsJUnit4TestCase.java:256)
	at org.apache.struts2.StrutsJUnit4TestCase.setUp(StrutsJUnit4TestCase.java:245)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: com.opensymphony.xwork2.config.ConfigurationException: Parent package is not defined: jinjava
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildPackageContext(XmlConfigurationProvider.java:689)
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:539)
	at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:326)
	at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:108)
	at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:196)
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
	... 42 common frames omitted

Unit Test (spring beans are configured via annotations, we are not using any xml)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LoginActionSpringConfig.class)
public class LoginActionTest extends IwsStrutsSpringTestCase&lt;LoginAction&gt; {

    @Test
    public void getActionMapping() {
        ActionMapping mapping = getActionMapping(&quot;/security/login&quot;);
        Assert.assertNotNull(mapping);
        Assert.assertEquals(&quot;/security&quot;, mapping.getNamespace());
        Assert.assertEquals(&quot;login&quot;, mapping.getName());
    }


}

Below is the IwsStrutsSpringTestCase super class that is extending StrutsSpringJUnit4TestCase. I have supplied a getConfigPath() including also attempting to put struts-plugin.xml in.

public abstract class IwsStrutsSpringTestCase&lt;T&gt; extends StrutsSpringJUnit4TestCase&lt;T&gt; {

    @Override
    protected String getConfigPath() {
        return &quot;struts.xml, struts-plugin.xml&quot;;
    }
}

Below is the struts-plugin.xml from the jinjava plugin that is being complained about in the stack trace

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;!DOCTYPE struts PUBLIC
        &quot;-//Apache Software Foundation//DTD Struts Configuration 2.5//EN&quot;
        &quot;http://struts.apache.org/dtds/struts-2.5.dtd&quot;&gt;

&lt;struts&gt;

    &lt;constant name=&quot;struts.jinjava.basepath&quot; value=&quot;WEB-INF/jinJava&quot; /&gt;

    &lt;package name=&quot;jinjava&quot; extends=&quot;struts-default&quot;&gt;
        &lt;result-types&gt;
            &lt;result-type name=&quot;jinjava&quot; class=&quot;com.hs.struts.result.JinJavaResult&quot; /&gt;
        &lt;/result-types&gt;
    &lt;/package&gt;
&lt;/struts&gt;

update 10/18/2020
pom.xml I would also like to note that the webapp directory is not following the default maven layout. The web files are stored under /web

update 11/1/2020
Below is my LoginActionSpringConfig.class as requested

@Configuration
public class LoginActionSpringConfig {

    @Bean
    public IIwsAuthenticationService authenticationService() {
        return new IwsAuthenticationServiceTest();
    }

    @Bean
    public ISecurityLogDao securityLogDao() {
        return new SecurityLogDaoTest();
    }

    @Bean
    public IUsersDepartmentDao usersDepartmentDao() {
        return new UsersDepartmentDaoTest();
    }

    @Bean
    public IDepartmentDao departmentDao() {
        return new DepartmentDaoTest();
    }

    @Bean
    public IwsCookieInterface iwsCookie() {
        return new IwsCookieTest();
    }

}

答案1

得分: 0

我不知道为什么上述配置不起作用,但我已经解决了最初的问题。我尝试重写了原始的getConfigPath(),因为我看到了这些FileNotFoundExceptions错误:

java.io.FileNotFoundException: 无法将类路径资源[WEB-INF/content/audit/]解析为URL,因为它不存在。

我正在使用struts-rest插件,该插件利用了convention插件,这些错误是由于convention插件引起的。我退了一步,去掉了getConfigPath()的覆盖。经过进一步的研究,我发现了一个stackoverflow帖子,该帖子建议在struts.xml中覆盖默认的struts.convention.result.path,并将其指向用于满足convention插件的操作类所在的包。我们在结果中没有以这种方式使用convention插件,因此这仅在单元测试期间用于满足convention插件。

<constant name="struts.convention.result.path" value="/com/hs/iws/actions" />

我现在遇到了不同的问题,但会开始一个新的帖子。

英文:

I don't know the specific answer on why the above configuration was not working, but I got past my initial problem. I tried overriding the getConfigPath() originally due to seeing these FileNotFoundExceptions

java.io.FileNotFoundException: class path resource [WEB-INF/content/audit/] cannot be resolved to URL because it does not exist

I am using the struts-rest plugin which utilizes the convention plugin which was throwing these errors. I took a step back and removed the getConfigPath() override. After some additional research and found a SO post that suggested overriding the default struts.convention.result.path in the struts.xml and pointing it at the package for the action classes to satisfy the convention plugin. We are not using the convention plugin in this way for our results, so this is only being done to satisfy the convention plugin during unit testing.

&lt;constant name=&quot;struts.convention.result.path&quot; value=&quot;/com/hs/iws/actions&quot; /&gt;

I am now experiencing a different issue, but will start a new post.

huangapple
  • 本文由 发表于 2020年10月17日 01:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64393937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定