使用StrutsSpringTestCase 测试框架报错:
- org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [struts.xml]; nested exception is java.net.ConnectException: Connection timed out: connect
- at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:408)
- at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
- at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
- at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
- at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
- at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
- at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
- at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
- at org.apache.struts2.StrutsSpringTestCase.setupBeforeInitDispatcher(StrutsSpringTestCase.java:39)
- at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:208)
- 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: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: java.net.ConnectException: Connection timed out: connect
依赖:
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-spring-plugin</artifactId>
- <version>2.3.1</version>
- </dependency>
- <!-- Struts JUnit Plugin -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-junit-plugin</artifactId>
- <version>2.3.15.3</version>
- </dependency>
测试类:
- package com.jn.test;
- import org.apache.struts2.StrutsSpringTestCase;
- import org.junit.Test;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.mock.web.MockHttpSession;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import com.opensymphony.xwork2.ActionProxy;
- import com.shop.jn.action.user.UserLoginAction;
- public class StrutsTest extends StrutsSpringTestCase{
- private ActionProxy proxy = null;
- private UserLoginAction loginAction = null;
- @Override
- protected String[] getContextLocations() {
- return new String[] {"struts.xml", "aop.xml","beans.xml","goods_beans.xml","supermarket_beans.xml","upload_beans.xml","user_beans.xml"};
- }
- /**
- * Spring与Struts集成的情况 Test method for
- * {@link com.springstrutsdemo.action.LoginAction#execute()}.
- *
- * @throws Exception
- */
- @Test
- public void testExecute() throws Exception {
- // request.setParameter("user.name", "age");
- // request.setParameter("user.age", "12");
- // request.setParameter("user.address", "北京市海淀区首钢国际大厦");
- // request.setParameter("user.telephone", "10010");
- MockHttpServletRequest request = new MockHttpServletRequest();
- MockHttpSession session = new MockHttpSession();
- request.setSession(session);
- RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
- request.setParameter("user.username", "aa");
- proxy = getActionProxy("/user/login");
- loginAction = (UserLoginAction) proxy.getAction();
- String result = proxy.execute();
- // String result=executeAction("/user/login")
- assertEquals("success", result);
- System.out.println("in test:user=" + loginAction.getUser());
- }
- }
根据错误信息,是在读取struts.xml时报错.
解决方法:修改struts.xml为:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "./com/struts-2.3.dtd">