当前位置: 代码迷 >> Java Web开发 >> 在StrutsTest中使用Tomcat数据源,该如何处理
  详细解决方案

在StrutsTest中使用Tomcat数据源,该如何处理

热度:334   发布时间:2016-04-17 13:31:20.0
在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)     {    
                                              super(testName);    
  相关解决方案