java—当我尝试测试使用从tomcat中的jndi连接轮询获得的连接的dao方法时,我没有得到initialContextException

72qzrwbm  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(250)

我有一个通过使用jndi查找获取连接的方法。
context.xml

  1. <Context>
  2. <Resource
  3. name="jdbc/UsersDB"
  4. auth="Container"
  5. type="javax.sql.DataSource"
  6. maxTotal="30"
  7. maxIdle="15"
  8. driverClassName="com.mysql.cj.jdbc.Driver"
  9. url="jdbc:mysql://localhost:3306/taksi_online"
  10. username="Bruce"
  11. password="givanchy"
  12. defaultAutoCommit="false"
  13. />
  14. </Context>

web.xml

  1. <resource-ref>
  2. <description>DB Connection</description>
  3. <res-ref-name>jdbc/UsersDB</res-ref-name>
  4. <res-type>javax.sql.DataSource</res-type>
  5. <res-auth>Container</res-auth>
  6. </resource-ref>
  7. public static void dataSourceInit() throws NamingException {
  8. Context initContext = new InitialContext();
  9. Context envContext = (Context) initContext.lookup("java:comp/env");
  10. dataSource = (DataSource) envContext.lookup("jdbc/UsersDB");
  11. }
  12. public static Connection getConnection() throws NamingException, SQLException {
  13. if(dataSource==null){
  14. dataSourceInit();
  15. }
  16. Connection connection = null;
  17. try {
  18. connection = dataSource.getConnection();
  19. } catch (SQLException e) {
  20. LOGGER.error(e);
  21. throw new SQLException(e);
  22. }
  23. return connection;
  24. }

我有一些简单的测试dao方法,它们使用方法getconnection

  1. class TaxiServiceCarTest {
  2. private OrderDao orderDao;
  3. private TaxiServiceOrder orderService;
  4. @BeforeEach
  5. void initTaxiService() {
  6. orderDao = mock(OrderDao.class);
  7. orderService = new TaxiServiceOrder(orderDao);
  8. }
  9. @Test
  10. void insertCar() throws MySQLEXContainer.MySQLDBLargeDataException, SQLException, MySQLEXContainer.MySQLDBExecutionException, ApplicationEXContainer.ApplicationCanNotChangeException {
  11. Order order = new Order();
  12. when(orderDao.insertOrder(any(Connection.class), any(Order.class))).thenReturn(true);
  13. assertTrue(orderService.insertOrder(order));
  14. }
  15. }

当我尝试运行此测试时,我没有得到initialContextException.ApplicationExcontainer,它是我的自定义异常。

  1. com.example.FinalProjectPM.db.exception.ApplicationEXContainer$ApplicationCanNotChangeException: 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
  2. at com.example.FinalProjectPM.db.services.TaxiServiceOrder.insertOrder(TaxiServiceOrder.java:30)
  3. at com.example.FinalProjectPM.db.services.TaxiServiceCarTest.insertCar(TaxiServiceCarTest.java:30)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  6. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  7. at java.lang.reflect.Method.invoke(Method.java:498)
  8. at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
  9. at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
  10. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
  11. at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
  12. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
  13. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
  14. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  15. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  16. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  17. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
  18. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
  19. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
  20. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
  21. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
  22. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
  23. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  24. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
  25. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
  26. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
  27. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
  28. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  29. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
  30. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  31. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
  32. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  33. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
  34. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
  35. at java.util.ArrayList.forEach(ArrayList.java:1259)
  36. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  37. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
  38. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  39. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
  40. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  41. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
  42. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  43. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
  44. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
  45. at java.util.ArrayList.forEach(ArrayList.java:1259)
  46. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  47. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
  48. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  49. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
  50. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  51. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
  52. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  53. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
  54. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
  55. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
  56. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
  57. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
  58. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
  59. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
  60. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
  61. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
  62. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
  63. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
  64. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
  65. at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
  66. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  67. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
  68. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
  69. Caused by: 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
  70. at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
  71. at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
  72. at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
  73. at javax.naming.InitialContext.lookup(InitialContext.java:417)
  74. at com.example.FinalProjectPM.db.mysql.MySQLDAOFactory.dataSourceInit(MySQLDAOFactory.java:21)
  75. at com.example.FinalProjectPM.db.mysql.MySQLDAOFactory.getConnection(MySQLDAOFactory.java:27)
  76. at com.example.FinalProjectPM.db.services.TaxiServiceOrder.insertOrder(TaxiServiceOrder.java:25)
  77. ... 66 more

为什么会出现此问题?我的webapp使用此连接轮询,因此我不认为我在配置上下文文件时出错,上下文似乎未注册。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题