2012-02-14 80 views
3

我有我想要的单元测试一个简单的Action类:Junit的测试Struts 2.x的行动

package com.gam.action.test; 
import org.apache.struts2.StrutsTestCase; 

public class HelloWorldActionTest extends StrutsTestCase{ 

    /** 
    * Test method for {@link com.gam.action.HelloWorldAction#execute()}. 
    */ 
    public void testExecute() { 
     fail("Not yet implemented"); 
    } 

} 

我创建使用JUnit向导在Eclipse这个测试用例。我运行测试时出现以下错误:

Class: com.opensymphony.xwork2.spring.SpringObjectFactory 
File: SpringObjectFactory.java 
Method: getClassInstance 
Line: 230 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1 
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:449) 
    at org.apache.struts2.util.StrutsTestCaseHelper.initDispatcher(StrutsTestCaseHelper.java:54) 
    at org.apache.struts2.StrutsTestCase.initDispatcher(StrutsTestCase.java:196) 
    at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:182) 
    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: java.lang.NullPointerException 
    at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:230) 
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:538) 
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:509) 
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:465) 
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:278) 
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112) 
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:204) 
    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:436) 
    ... 17 more 

问题是我不知道应该提供哪些jar文件。我没有在我的项目中使用spring,但如果我不提供弹簧jar文件,我会得到一些错误,当我提供它时,我会得到这个。 需要什么样的jar文件组合才能运行测试! (正如你可以看到我已经降级我的测试方法,以一个虚拟的方法。)

回答

1

Struts2的-的JUnit插件推出了自己的依赖,这个Maven的输出显示:

[INFO] +- org.apache.struts:struts2-junit-plugin:jar:2.3.1:compile 
[INFO] | +- org.springframework:spring-test:jar:3.0.5.RELEASE:compile 
[INFO] | +- org.springframework:spring-core:jar:3.0.5.RELEASE:compile 
[INFO] | | +- org.springframework:spring-asm:jar:3.0.5.RELEASE:compile 
[INFO] | | \- commons-logging:commons-logging:jar:1.1.1:compile 
[INFO] | +- org.springframework:spring-context:jar:3.0.5.RELEASE:compile 
[INFO] | | +- org.springframework:spring-aop:jar:3.0.5.RELEASE:compile 
[INFO] | | | \- aopalliance:aopalliance:jar:1.0:compile 
[INFO] | | +- org.springframework:spring-beans:jar:3.0.5.RELEASE:compile 
[INFO] | | \- org.springframework:spring-expression:jar:3.0.5.RELEASE:compile 
[INFO] | \- junit:junit:jar:4.8.2:compile 

听起来就像你没有使用Maven,这几乎肯定是一个糟糕的想法。自己管理传递依赖并不是非常有趣,节省了你一些时间和体力劳动。

+0

感谢戴夫的帮助。 – LAC 2012-02-14 16:19:43