日期:2014-05-18  浏览次数:20662 次

在StrutsTest中使用Tomcat数据源
在做Struts的单元测试时我是使用的StrutsTest作为JUnit的扩展。    
但是在测试Action时会去访问数据库,而我的数据库是使用的Tomcat配置的数据源。    
这样就会出现一个异常javax.naming.NoInitialContextException:     Need     to     specify     class     name     in     environment     or     system     property,     or     as     an     applet     parameter,     or     in     an     application     resource     file:         java.naming.factory.initial    
如何加载Tomcat的配置文件,并且使用Tomcat数据源呢?    
 
 
测试代码:    
package     com.xxxxxx.prework.actions;    
 
import     java.io.File;    
 
import     servletunit.struts.MockStrutsTestCase;    
 
public     class     TestLoginAction     extends     MockStrutsTestCase     {    
 
                      @Override    
                      protected     void     setUp()     throws     Exception     {    
                                              super.setUp();    
                                              File     contextFile     =     new     File(    
                                                                                              "C:\\eclipse\\eclipse     europa\\workspace\\Prework\\WebContent ");    
                                              this.setContextDirectory(contextFile);    
                      }    
 
                      @Override    
                      protected     void     tearDown()     throws     Exception     {    
                                              super.tearDown();    
                      }    
 
                      public     TestLoginAction(String     testName)