From 8b4f5bac659cbf81bbdf0d831be5f6bf86dc5085 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 5 Jun 2026 21:17:12 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E9=87=8D=E6=9E=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=B1=BB=20`UpdateTest`=EF=BC=8C=E8=A1=A5=E5=85=A8=20Statement?= =?UTF-8?q?=20=E8=B7=AF=E5=BE=84=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 按 update / updateAndReturnKeys 分组,内部再按 PreparedStatement (有参数)和 Statement(无参数)子路径组织,提升可维护性 - 重命名 3 个测试方法以消除歧义: testUpdateWithNullParams → testUpdateWithNullElement testUpdateWithParamsIsNull → testUpdateWithParamsNull testUpdateAndReturnKeysWithParamsIsNull → testUpdateAndReturnKeysWithParamsNull - 新增 2 个测试用例补全 params=null 的 Statement 路径覆盖: testUpdateWithParamsNull testUpdateAndReturnKeysWithParamsNull --- .../java/xyz/zhouxy/jdbc/test/UpdateTest.java | 110 +++++++++++------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/src/test/java/xyz/zhouxy/jdbc/test/UpdateTest.java b/src/test/java/xyz/zhouxy/jdbc/test/UpdateTest.java index 24b9a0e..b08885f 100644 --- a/src/test/java/xyz/zhouxy/jdbc/test/UpdateTest.java +++ b/src/test/java/xyz/zhouxy/jdbc/test/UpdateTest.java @@ -21,6 +21,8 @@ import xyz.zhouxy.jdbc.SimpleJdbcTemplate; /** * 更新 API 测试:update、updateAndReturnKeys。 + * + *

内部按 PreparedStatement(有参数)和 Statement(无参数)路径组织。

*/ @DisplayName("SimpleJdbcTemplate 更新操作") class UpdateTest extends BaseH2Test { @@ -33,6 +35,7 @@ class UpdateTest extends BaseH2Test { } // ==================== update ==================== + // --- PreparedStatement 路径(有参数) --- @Test @DisplayName("update:INSERT 操作返回影响行数 1") @@ -121,54 +124,18 @@ class UpdateTest extends BaseH2Test { } @Test - @DisplayName("update:DELETE 全表") - void testUpdateDeleteAll() throws SQLException { - SimpleJdbcTemplate template = createTemplate(); - - int rows = template.update("DELETE FROM users"); - - logger.info("DELETE 全表影响行数: {}", rows); - assertEquals(5, rows); - - // 验证表为空 - int count = template.query("SELECT COUNT(*) FROM users", - rs -> { rs.next(); return rs.getInt(1); }); - assertEquals(0, count); - } - - @Test - @DisplayName("update:null 参数数组") - void testUpdateWithNullParams() throws SQLException { + @DisplayName("update:参数中包含 null 元素") + void testUpdateWithNullElement() throws SQLException { SimpleJdbcTemplate template = createTemplate(); int rows = template.update( "DELETE FROM users WHERE username = ?", new Object[]{ null }); - // 因为 DELETE ? 中参数 null 不会匹配任何行 + // 参数 null 不匹配任何行 assertEquals(0, rows); } - @Test - @DisplayName("update:无参数重载") - void testUpdateNoParams() throws SQLException { - SimpleJdbcTemplate template = createTemplate(); - - int rows = template.update( - "UPDATE users SET balance = 9999 WHERE username = 'alice'"); - - assertEquals(1, rows); - } - - @Test - @DisplayName("update:语法错误抛出 SQLException") - void testUpdateInvalidSql() { - SimpleJdbcTemplate template = createTemplate(); - - assertThrows(SQLException.class, () -> - template.update("UPDAT users SET x = 1")); - } - @Test @DisplayName("update:使用 Instant 类型参数填充 TIMESTAMP 列") void testUpdateWithInstantParam() throws SQLException { @@ -195,7 +162,56 @@ class UpdateTest extends BaseH2Test { assertEquals(java.sql.Timestamp.from(now), stored); } + // --- update / Statement 路径(无参数) --- + + @Test + @DisplayName("update:无参数重载") + void testUpdateNoParams() throws SQLException { + SimpleJdbcTemplate template = createTemplate(); + + int rows = template.update( + "UPDATE users SET balance = 9999 WHERE username = 'alice'"); + + assertEquals(1, rows); + } + + @Test + @DisplayName("update:DELETE 全表(无参数)") + void testUpdateDeleteAll() throws SQLException { + SimpleJdbcTemplate template = createTemplate(); + + int rows = template.update("DELETE FROM users"); + + logger.info("DELETE 全表影响行数: {}", rows); + assertEquals(5, rows); + + // 验证表为空 + int count = template.query("SELECT COUNT(*) FROM users", + rs -> { rs.next(); return rs.getInt(1); }); + assertEquals(0, count); + } + + @Test + @DisplayName("update:params 为 null,走 Statement 路径") + void testUpdateWithParamsNull() throws SQLException { + SimpleJdbcTemplate template = createTemplate(); + + int rows = template.update("DELETE FROM users", (Object[]) null); + + assertEquals(5, rows); + } + + @Test + @DisplayName("update:语法错误抛出 SQLException") + void testUpdateInvalidSql() { + SimpleJdbcTemplate template = createTemplate(); + + assertThrows(SQLException.class, () -> + template.update("UPDAT users SET x = 1")); + } + // ==================== updateAndReturnKeys ==================== + // --- PreparedStatement 路径(有参数) --- @Test @DisplayName("updateAndReturnKeys:INSERT 返回自增主键") @@ -231,6 +247,8 @@ class UpdateTest extends BaseH2Test { assertEquals(2, keys.size()); } + // --- updateAndReturnKeys / Statement 路径(无参数) --- + @Test @DisplayName("updateAndReturnKeys:无参数重载") void testUpdateAndReturnKeysNoParams() throws SQLException { @@ -243,4 +261,18 @@ class UpdateTest extends BaseH2Test { assertEquals(1, keys.size()); assertTrue(keys.get(0) > 0); } + + @Test + @DisplayName("updateAndReturnKeys:params 为 null,走 Statement 路径") + void testUpdateAndReturnKeysWithParamsNull() throws SQLException { + SimpleJdbcTemplate template = createTemplate(); + + RowMapper rowMapper = (rs, rowNumber) -> rs.getLong(1); + List keys = template.updateAndReturnKeys( + "INSERT INTO users (username) VALUES ('null_test')", + null, rowMapper); + + assertEquals(1, keys.size()); + assertTrue(keys.get(0) > 0); + } }