Java中的DB连接测试问题 [英] Problem with Db connection testing in java

查看:89
本文介绍了Java中的DB连接测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试我的Dao类,但是它为类DbConnection返回此错误:

I tried to testing my Dao class but it return this error for the class DbConnection:

javax.naming.NoInitialContextException:需要在环境或系统属性或应用程序资源文件中指定类名称:java.naming.factory.initial 在java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:691) 在java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305) 在java.naming/javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342) 在java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409) 在model.DbConnection.(DbConnection.java:16) 在model.DbConnection.getInstance(DbConnection.java:30) 在model.ProfileManager.ReturnPatientByKey(ProfileManager.java:27) 在model.ProfileManagerTest.testReturnPatientByKey(ProfileManagerTest.java:32)处 在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base/java.lang.reflect.Method.invoke(Method.java:564) ....

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:691) at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305) at java.naming/javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342) at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409) at model.DbConnection.(DbConnection.java:16) at model.DbConnection.getInstance(DbConnection.java:30) at model.ProfileManager.ReturnPatientByKey(ProfileManager.java:27) at model.ProfileManagerTest.testReturnPatientByKey(ProfileManagerTest.java:32) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) ....

我的DbConnection类:

my DbConnection class:

package model;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DbConnection {

    private DbConnection() {

        Context ctx;
        try {
            ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/CheckUpDb");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public Connection getConnection() throws SQLException {
        return ds.getConnection();

    }

    public static DbConnection getInstance () {
        if(db==null) {
            return new DbConnection();
        }
        else {
            return db;
        }
    } 

    private static DataSource ds;
    private static DbConnection db;
}

数据库连接在Web应用程序中有效,只有测试返回此错误. 我认为问题不在我的类ProfileManager上,因为它只是一个测试示例:

Database connection works in the web application, only the testing return this error. I don't think the problem si my class ProfileManager because it is only a testing example:


import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import model.Bean.PatientBean;

class ProfileManagerTest{

    private ProfileManager pm;
    String fiscal_code;
    String user_password;
    PatientBean patient;
    
    @BeforeEach
    void setUp() throws Exception {
        this.pm = new ProfileManager();
        fiscal_code = "aaa";
        user_password = "bbb";
        patient = mock(PatientBean.class);
    }

    @AfterEach
    void tearDown() throws Exception {
    }

    @Test
    void testReturnPatientByKey() {
        patient = (PatientBean) pm.ReturnPatientByKey(fiscal_code, user_password);
        assertTrue(true);
    }

}

推荐答案

数据库连接在Web应用程序中有效,只有测试返回此错误.

Database connection works in the web application, only the testing return this error.

最有可能是因为您在服务器配置中声明了数据源,并且服务器正在为您的Web应用程序提供一个数据源,但是您在测试中没有完成相同的工作.

That's most likely because you have the datasource declared in the server configuration and the server is providing your web app with one, but you don't have the same done in your test.

在服务器文件中进行搜索,您可能会发现类似或类似的内容,具体取决于您所使用的服务器:

Do a search in your server files an you will probably discover something like this, or similar, depending on what server you use:

<Resource name="jdbc/CheckUpDb" 
  driverClassName="..." 
  type="..." 
  url="..." 
  username="..." 
  password="..." 
/>

这是使用JNDI配置数据源的方法.当您的Web应用程序运行时,服务器将按名称为您提供此资源.这就是ctx.lookup("java:comp/env/jdbc/CheckUpDb");的作用.它要求服务器为它提供该资源.

This is a way to configure a datasource using JNDI. When your web application runs, the server will provide you with this resource by name. This is what ctx.lookup("java:comp/env/jdbc/CheckUpDb"); does. It asks the server to give it that resource.

但是,当您运行单元测试时,您将在服务器环境之外运行.这意味着在运行测试时,您在服务器中定义的任何资源(例如,使用context.xml)都不存在.在测试中,您必须提供一个数据源并使它可用于您的JNDI上下文,以便此行代码可以正常工作:

But when you run your unit tests, you run outside the server environment. That means that any resource you defined in the server (using context.xml for example) doesn't exist when you run your tests. In your tests you have to provide a datasource and make it available to your JNDI context so that this line of code then works:

ds = (DataSource)ctx.lookup("java:comp/env/jdbc/CheckUpDb");

以下文章应为您提供必要的详细信息,以设置用于测试的JNDI数据源:

The following post should give you the necessary details to set up your JNDI data source for your test: Setting up JNDI Datasource in jUnit

您将看到那里的示例使用了一个名为Simple-Jndi的库,它们用于提供JNDI上下文并将其配置为包括测试然后尝试按名称检索的数据源.您可以使用任何JNDI提供程序,但是必须自己(在@BeforeEach@BeforeAll内部)设置用于测试的数据源,因为在运行单元测试时,您没有tomcat服务器为您执行此操作.

You will see that the examples there make use of a library called Simple-Jndi that they use to provide a JNDI context and configure it to include the datasource that the tests then try to retrieve by name. You can use any JNDI provider, but you must set up the datasource for your test yourself (inside @BeforeEach or @BeforeAll) because when running unit tests, you don't have the tomcat server doing this for you.

这篇关于Java中的DB连接测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆