test: add unit test cases
This commit is contained in:
		
							parent
							
								
									5a54ab4ab9
								
							
						
					
					
						commit
						425b689d81
					
				
							
								
								
									
										
											BIN
										
									
								
								lib/hamcrest-core-1.3.jar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								lib/hamcrest-core-1.3.jar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								lib/junit-4.13.2.jar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								lib/junit-4.13.2.jar
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										81
									
								
								src/test/util/DatabaseUtilTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								src/test/util/DatabaseUtilTest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,81 @@ | |||||||
|  | package test.util; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | import util.DatabaseUtil; | ||||||
|  | 
 | ||||||
|  | import java.sql.Connection; | ||||||
|  | import java.sql.SQLException; | ||||||
|  | 
 | ||||||
|  | import static org.junit.Assert.*; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * DatabaseUtil类的单元测试 | ||||||
|  |  */ | ||||||
|  | public class DatabaseUtilTest { | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 测试获取数据库连接 | ||||||
|  |      */ | ||||||
|  |     @Test | ||||||
|  |     public void testGetConnection() { | ||||||
|  |         try (Connection conn = DatabaseUtil.getConnection()) { | ||||||
|  |             // 验证连接不为空 | ||||||
|  |             assertNotNull("数据库连接不应该为空", conn); | ||||||
|  |             // 验证连接是否有效 | ||||||
|  |             assertTrue("数据库连接应该是有效的", !conn.isClosed()); | ||||||
|  |         } catch (SQLException e) { | ||||||
|  |             fail("获取数据库连接时发生异常: " + e.getMessage()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 测试连接自动关闭 | ||||||
|  |      */ | ||||||
|  |     @Test | ||||||
|  |     public void testConnectionAutoClose() { | ||||||
|  |         Connection conn = null; | ||||||
|  |         try { | ||||||
|  |             // 在try-with-resources块中获取连接 | ||||||
|  |             try (Connection autoCloseConn = DatabaseUtil.getConnection()) { | ||||||
|  |                 assertNotNull("数据库连接不应该为空", autoCloseConn); | ||||||
|  |                 assertTrue("连接应该是开启状态", !autoCloseConn.isClosed()); | ||||||
|  |             } | ||||||
|  |             // try-with-resources块结束后,获取一个新连接来测试 | ||||||
|  |             conn = DatabaseUtil.getConnection(); | ||||||
|  |             assertNotNull("新的数据库连接不应该为空", conn); | ||||||
|  |             assertTrue("新的连接应该是开启状态", !conn.isClosed()); | ||||||
|  |         } catch (SQLException e) { | ||||||
|  |             fail("测试连接自动关闭时发生异常: " + e.getMessage()); | ||||||
|  |         } finally { | ||||||
|  |             if (conn != null) { | ||||||
|  |                 try { | ||||||
|  |                     conn.close(); | ||||||
|  |                 } catch (SQLException e) { | ||||||
|  |                     e.printStackTrace(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 测试多次获取连接 | ||||||
|  |      */ | ||||||
|  |     @Test | ||||||
|  |     public void testMultipleConnections() { | ||||||
|  |         try (Connection conn1 = DatabaseUtil.getConnection(); | ||||||
|  |              Connection conn2 = DatabaseUtil.getConnection()) { | ||||||
|  |              | ||||||
|  |             assertNotNull("第一个数据库连接不应该为空", conn1); | ||||||
|  |             assertNotNull("第二个数据库连接不应该为空", conn2); | ||||||
|  |              | ||||||
|  |             // 验证获取到的是两个不同的连接对象 | ||||||
|  |             assertNotEquals("两个连接对象应该是不同的实例", conn1, conn2); | ||||||
|  |              | ||||||
|  |             // 验证两个连接都是有效的 | ||||||
|  |             assertTrue("第一个连接应该是有效的", !conn1.isClosed()); | ||||||
|  |             assertTrue("第二个连接应该是有效的", !conn2.isClosed()); | ||||||
|  |         } catch (SQLException e) { | ||||||
|  |             fail("测试多个连接时发生异常: " + e.getMessage()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user