2012-02-14 64 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