一旦部署在 Tomcat 服务器上,应用程序、数据库连接和 SQL 查询就可以正常工作。尝试在应用程序上运行一些 JUnit 测试并出现以下错误:
错误 [main] (DbConnectionTest.java:35) - 需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中:java.naming.factory.initial
这是 JUnit 类
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DbConnectionTest {
private static Logger logger = Logger.getLogger(DbConnection.class);
private static Connection con;
@BeforeClass
public static void setUpClass() throws Exception{
logger.info("Test started from DbConnectionTest...");
con = null;
}
@Test
public void testGetConnection() {
System.out.println("Running the getConnection");
try{
con = DbConnection.getConnection();
}
catch(Exception e){
logger.error(e.getMessage());
fail("Failed to connect to the database");
}
}
@AfterClass
public static void tearDownClass() throws Exception {
logger.info("Test Ended DbConnectionTest...");
if(con != null){
con.close();
}
}
}
这是它正在测试的方法:
public static Connection getConnection() throws SQLException, NamingException {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/db");
Connection conn = ds.getConnection();
return conn;
}
当 MariaDB 部署在 Tomcat 上时,我有一个 context.xml 和 web.xml 文件用于连接到 MariaDB。不确定使用 JUnit 时出现的问题。
编辑
现在出现这个错误:
java.lang.NoClassDefFoundError:org/apache/juli/logging/LogFactory
这个方法导致的
public static Connection getConnection() throws SQLException, NamingException {
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext(); //Line causing error
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");
// Construct DataSource
MariaDbDataSource ds = new MariaDbDataSource();
ds.setUrl("jdbc:mariadb://localhost:3306");
ds.setUser("user");
ds.setPassword("pass");
ic.bind("java:/comp/env/jdbc/db", ds);
Connection conn = ds.getConnection();
return conn;
}
我正在使用 Eclipse,这是堆栈跟踪
java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.apache.naming.NamingContext.<clinit>(NamingContext.java:60)
at org.apache.naming.java.javaURLContextFactory.getInitialContext(javaURLContextFactory.java:112)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at com.prudential.connection.DbConnection.getConnection(DbConnection.java:35)
at com.prudential.tests.DbConnectionTest.testGetConnection(DbConnectionTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
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:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 33 more
最佳答案
您正在指定 JNDI 查找,如果您在没有 JNDI 上下文的情况下使用 JUnit,它将无法工作。
看看这个:
https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit
您可以创建您的连接,并将其放入带有 @Before
注释方法的测试 JNDI 上下文中的正确位置,这样当您需要查找它时它就存在。
@BeforeClass
public static void setUpClass() throws Exception{
logger.info("Test started from DbConnectionTest...");
con = null;
logger.info( "TODO: I need to create a database connection and JNDI
"context and register a connection here, so that when my" +
" method tries to look it up it will work!"
);
}
关于java - 使用 JUnit 连接到数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960224/