java中db,Java中的db连接测试问题

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

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)

....

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;

}

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);

}

}

解决方案

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

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:

driverClassName="..."

type="..."

url="..."

username="..."

password="..."

/>

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.

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");

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值