Jdbc jUnit数据源问题

Jdbc jUnit数据源问题,jdbc,junit,datasource,Jdbc,Junit,Datasource,我试图打印数据库表名,但由于某种原因,当我尝试测试该方法时,我得到了(如下所示) javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中指定:java.naming.factory.initial 我已经查过了,显然你必须在测试类中手动设置连接,否则它将不起作用(但是,我读到的这个主题是从2008年开始的)。有什么办法我不能那样做吗 public class ConnectionPool {

我试图打印数据库表名,但由于某种原因,当我尝试测试该方法时,我得到了(如下所示)

javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中指定:java.naming.factory.initial

我已经查过了,显然你必须在测试类中手动设置连接,否则它将不起作用(但是,我读到的这个主题是从2008年开始的)。有什么办法我不能那样做吗

public class ConnectionPool {
    private static ConnectionPool pool = null;
    private static DataSource dataSource = null;

private ConnectionPool(){
    try{
        InitialContext cx = new InitialContext();
        this.dataSource = (DataSource) cx.lookup("java:/comp/env/jdbc/tut_web");
    } catch (NamingException ex) {
        Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static synchronized ConnectionPool getInstance(){
    if(pool == null){
        pool = new ConnectionPool();
    }

    return pool;
}

public Connection getConnection(){
    try{
        return this.dataSource.getConnection();
    } catch (SQLException ex) {
        Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public void closeConnection(Connection connection){
    try {
        connection.close();
    } catch (SQLException ex) {
        Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}
Context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
   <Resource name="jdbc/tut_web" auth="Container"
       driverClassName="com.mysql.jdbc.Driver"
       url="jdbc:mysql://localhost:3306/tut_web?autoReconnect=true"
       username="root" password="password"
       maxActive="100" maxIdle="30" maxWait="10000"
       logAbandoned="true" removeAbandoned="true"
       removeAbandonedTimeout="60" type="javax.sql.DataSource" />
</Context>

Stacktree:

Apr 20, 2018 5:40:11 PM com.vio.db.config.ConnectionPool <init>
SEVERE: null
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
 at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.vio.db.config.ConnectionPool.<init>(ConnectionPool.java:27)
at com.vio.db.config.ConnectionPool.getInstance(ConnectionPool.java:35)
at com.vio.repository.CRUDReflection.<init>(CRUDReflection.java:27)
at com.vio.service.CRUDReflectionTEST.setUp(CRUDReflectionTEST.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.tools.ant.taskdefs.optional.junit.JUnit4TestMethodAdapter.run(JUnit4TestMethodAdapter.java:107)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:535)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
2018年4月20日下午5:40:11 com.vio.db.config.ConnectionPool
严重:空
javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中指定类名:java.naming.factory.initial
位于javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
位于javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
位于javax.naming.InitialContext.GetUrlOrDefaultiniticTx(InitialContext.java:350)
在javax.naming.InitialContext.lookup(InitialContext.java:417)
位于com.vio.db.config.ConnectionPool(ConnectionPool.java:27)
在com.vio.db.config.ConnectionPool.getInstance(ConnectionPool.java:35)上
位于com.vio.repository.CRUDReflection(CRUDReflection.java:27)
位于com.vio.service.CRUDReflectionTEST.setUp(CRUDReflectionTEST.java:38)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
位于org.junit.internal.runners.statements.runafter.evaluate(runafter.java:27)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
位于org.junit.internal.runners.statements.runafter.evaluate(runafter.java:27)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:363)
位于org.apache.tools.ant.taskdefs.optional.junit.JUnit4TestMethodAdapter.run(JUnit4TestMethodAdapter.java:107)
位于org.apache.tools.ant.taskdefs.optional.junit.junitestrunner.run(junitestrunner.java:535)
位于org.apache.tools.ant.taskdefs.optional.junit.junitestrunner.launch(junitestrunner.java:1182)
位于org.apache.tools.ant.taskdefs.optional.junit.junitestrunner.main(junitestrunner.java:1033)

您使用的是Spring吗?不是。我使用的是Servlets。您的问题是,在运行JUnit测试时,您不在服务器环境中。为了避免这样做,您的连接初始化应该集中。然后,您可以在测试环境中覆盖它,因为它不使用服务器中配置的上下文。