单元测试由Junit4变更为Junit5

This commit is contained in:
Looly
2024-08-09 14:53:02 +08:00
parent 994ac87ae6
commit 85fc13be04
14 changed files with 87 additions and 61 deletions

View File

@@ -160,10 +160,11 @@
<version>1.7.36</version>
<scope>test</scope>
</dependency>
<!-- H2数据库版本固定2.2.x 支持到JDK8 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.3.230</version>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.db.handler.EntityListHandler;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -22,7 +22,7 @@ public class ConcurentTest {
private Db db;
@Before
@BeforeEach
public void init() {
db = Db.use("test");
}

View File

@@ -1,13 +1,14 @@
package cn.hutool.db;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Derby数据库单元测试
*
@@ -18,7 +19,7 @@ public class DerbyTest {
private static final String DS_GROUP_NAME = "derby";
@BeforeClass
@BeforeAll
public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME);
try{

View File

@@ -1,12 +1,12 @@
package cn.hutool.db;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* 达梦数据库单元测试
*
@@ -16,7 +16,7 @@ public class DmTest {
private static final String DS_GROUP_NAME = "dm";
@BeforeClass
//@BeforeAll
public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b INTEGER)");

View File

@@ -1,13 +1,14 @@
package cn.hutool.db;
import cn.hutool.db.pojo.User;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Entity测试
*
@@ -18,7 +19,7 @@ public class FindBeanTest {
Db db;
@Before
@BeforeEach
public void init() {
db = Db.use("test");
}

View File

@@ -2,14 +2,15 @@ package cn.hutool.db;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.map.MapUtil;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* H2数据库单元测试
*
@@ -20,7 +21,7 @@ public class H2Test {
private static final String DS_GROUP_NAME = "h2";
@BeforeClass
@BeforeAll
public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");

View File

@@ -1,12 +1,13 @@
package cn.hutool.db;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* HSQLDB数据库单元测试
*
@@ -17,7 +18,7 @@ public class HsqldbTest {
private static final String DS_GROUP_NAME = "hsqldb";
@BeforeClass
@BeforeAll
public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");

View File

@@ -1,22 +1,22 @@
package cn.hutool.db;
import cn.hutool.core.lang.Console;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* MySQL操作单元测试
*
* @author looly
*/
public class MySQLTest {
@BeforeClass
@Disabled
//@BeforeAll
public static void createTable() throws SQLException {
Db db = Db.use("mysql");
db.executeBatch("drop table if exists testuser", "CREATE TABLE if not exists `testuser` ( `id` int(11) NOT NULL, `account` varchar(255) DEFAULT NULL, `pass` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
@@ -38,20 +38,20 @@ public class MySQLTest {
/**
* 事务测试<br>
* 更新三条信息低2条后抛出异常正常情况下三条都应该不变
*
* @throws SQLException SQL异常
*/
@Test(expected = SQLException.class)
@Test
@Disabled
public void txTest() throws SQLException {
Db.use("mysql").tx(db -> {
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
if (1 == update) {
// 手动指定异常,然后测试回滚触发
throw new RuntimeException("Error");
}
db.update(Entity.create("user").set("text", "描述102"), Entity.create().set("id", 102));
public void txTest() {
assertThrows(SQLException.class, () -> {
Db.use("mysql").tx(db -> {
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
if (1 == update) {
// 手动指定异常,然后测试回滚触发
throw new RuntimeException("Error");
}
db.update(Entity.create("user").set("text", "描述102"), Entity.create().set("id", 102));
});
});
}
@@ -79,6 +79,6 @@ public class MySQLTest {
db.upsert(Entity.create("testuser").set("id", 1).set("account", "icefairy").set("pass", "a123456"));
Entity user = db.get(Entity.create("testuser").set("id", 1));
System.out.println("user======="+user.getStr("account")+"___"+user.getStr("pass"));
assertEquals(user.getStr("account"), new String("icefairy"));
assertEquals(user.getStr("account"), "icefairy");
}
}

View File

@@ -1,17 +1,19 @@
package cn.hutool.db;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UpdateTest {
Db db;
@Before
@BeforeEach
public void init() {
db = Db.use("test");
}

View File

@@ -4,12 +4,13 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.ds.DSFactory;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* 元数据信息单元测试
*
@@ -46,9 +47,11 @@ public class MetaUtilTest {
/**
* 表不存在抛出异常。
*/
@Test(expected = DbRuntimeException.class)
@Test
public void getTableNotExistTest() {
final Table table = MetaUtil.getTableMeta(ds, "user_not_exist");
assertEquals(table.getIndexInfoList().size(), 2);
assertThrows(DbRuntimeException.class, () -> {
final Table table = MetaUtil.getTableMeta(ds, "user_not_exist");
assertEquals(table.getIndexInfoList().size(), 2);
});
}
}