Boa noite pessoal,
Estou tentando realizar testes unitários no struts 2, porém não consegui fazer nada ainda. Se alguém puder me dar uma mão eu agradeço.
Antes que alguém diga “peça para o ‘pai google’ baixar a resposta”, já dei uma olhada e sai mais confuso do que antes. Tipo tem uns códigos como este aqui para se fazer o teste:
package jubernate.actions;
import java.util.HashMap;
import junit.framework.TestCase;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.views.JspSupportServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ContextLoader;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
/**
* @author Zarar Siddiqi
*/
public abstract class BaseStrutsTestCase extends TestCase {
//Localização do ApplicationContext do Spring
private static final String CONFIG_LOCATIONS = "file:C:\\Users\\Juba\\Documents\\Novos_PrOgRaMaS\\jubernate\\src\\main\\webapp\\WEB-INF\\applicationContext.xml";
private static ApplicationContext applicationContext;
private Dispatcher dispatcher;
protected ActionProxy proxy;
protected static MockServletContext servletContext;
protected static MockServletConfig servletConfig;
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
public BaseStrutsTestCase(String name) {
super(name);
}
/**
* Created action class based on namespace and name
*
* @param clazz
* Class for which to create Action
* @param namespace
* Namespace of action
* @param name
* Action name
* @return Action class
* @throws Exception
* Catch-all exception
*/
@SuppressWarnings("unchecked")
protected <T> T createAction(Class<T> clazz, String namespace, String name)
throws Exception {
// create a proxy class which is just a wrapper around the action call.
// The proxy is created by checking the namespace and name against the
// struts.xml configuration
proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class)
.createActionProxy(namespace, name, null, true, false);
// by default, don't pass in any request parameters
proxy.getInvocation().getInvocationContext()
.setParameters(new HashMap());
// do not execute the result after executing the action
proxy.setExecuteResult(true);
// set the actions context to the one which the proxy is using
ServletActionContext.setContext(proxy.getInvocation()
.getInvocationContext());
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ServletActionContext.setServletContext(servletContext);
return (T) proxy.getAction();
}
protected void setUp() throws Exception {
System.out.println(System.getProperties());
if (applicationContext == null) {
// this is the first time so initialize Spring context
servletContext = new MockServletContext();
servletContext.addInitParameter(
ContextLoader.CONFIG_LOCATION_PARAM, CONFIG_LOCATIONS);
applicationContext = (new ContextLoader())
.initWebApplicationContext(servletContext);
// Struts JSP support servlet (for Freemarker)
new JspSupportServlet().init(new MockServletConfig(servletContext));
}
// Dispatcher is the guy that actually handles all requests. Pass in
// an empty. Map as the parameters but if you want to change stuff like
// what config files to read, you need to specify them here. Here's how
// to
// scan packages for actions (thanks to Hardy Ferentschik - Comment 66)
// (see Dispatcher's source code)
HashMap params = new HashMap();
params.put("actionPackages", "jubernate.actions");
//Penei para descobrir quue era assim que eu indicava o lugar do meu struts.xml
params.put(
"config",
"file:C:\\Users\\Juba\\Documents\\Novos_PrOgRaMaS\\jubernate\\src\\main\\resources\\struts.xml");
dispatcher = new Dispatcher(servletContext, params);
dispatcher.init();
Dispatcher.setInstance(dispatcher);
}
}
Segundo o que li era só extender esta classe abstrata na classe de teste e pimba… Mas não foi assim… Primeiro dava um erro para localizar o ApplicationContext.xml - Depois de uma hora problema resolvido (tinha de referenciar a localização física do arquivo e utilizar a palavra “file” antes do endereço), depois o mesmo problema para o struts.xml, só que dessa vez tinha de por dentro Map. Agora tá dando este erro:
2012-12-10 23:13:46,945 ERROR org.apache.struts2.dispatcher.Dispatcher.error:38 - Dispatcher initialization failed
com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setObjectFactory(com.opensymphony.xwork2.ObjectFactory).
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:144)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:86)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:67)
at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:490)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:530)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:528)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:580)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:528)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:233)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at jubernate.actions.BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:106)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setObjectFactory(com.opensymphony.xwork2.ObjectFactory).
at com.opensymphony.xwork2.inject.ContainerImpl.createParameterInjector(ContainerImpl.java:239)
at com.opensymphony.xwork2.inject.ContainerImpl.getParametersInjectors(ContainerImpl.java:229)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.<init>(ContainerImpl.java:293)
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:117)
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:114)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:141)
... 34 more
Dando uma olhada dentro dos fontes, descobri que tem uma classe chamada org.apache.struts2.dispatcher.Dispatcher e que as configurações dela estão nulas (configurationManager.getConfiguration() == null).
Bom… como disse se alguém souber como testar o struts2 e poder compartilhar comigo ou souber como corrigo este erro, eu agradeço e muito.
Inté…