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<LoginAction> {
@Test
public void getActionMapping() {
ActionMapping mapping = getActionMapping("/security/login");
Assert.assertNotNull(mapping);
Assert.assertEquals("/security", mapping.getNamespace());
Assert.assertEquals("login", 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<T> extends StrutsSpringJUnit4TestCase<T> {
@Override
protected String getConfigPath() {
return "struts.xml, struts-plugin.xml";
}
}
Below is the struts-plugin.xml from the jinjava plugin that is being complained about in the stack trace
<?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>
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();
}
}
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.
<constant name="struts.convention.result.path" value="/com/hs/iws/actions" />
I am now experiencing a different issue, but will start a new post.