单元测试由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> <version>1.7.36</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- H2数据库版本固定2.2.x 支持到JDK8 -->
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>2.3.230</version> <version>2.2.224</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

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

View File

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

View File

@ -1,12 +1,12 @@
package cn.hutool.db; 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.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.SQLException; 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"; private static final String DS_GROUP_NAME = "dm";
@BeforeClass //@BeforeAll
public static void init() throws SQLException { public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME); Db db = Db.use(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b INTEGER)"); db.execute("CREATE TABLE test(a INTEGER, b INTEGER)");

View File

@ -1,13 +1,14 @@
package cn.hutool.db; package cn.hutool.db;
import cn.hutool.db.pojo.User; import cn.hutool.db.pojo.User;
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* Entity测试 * Entity测试
* *
@ -18,7 +19,7 @@ public class FindBeanTest {
Db db; Db db;
@Before @BeforeEach
public void init() { public void init() {
db = Db.use("test"); 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.CaseInsensitiveMap;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeAll;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* H2数据库单元测试 * H2数据库单元测试
* *
@ -20,7 +21,7 @@ public class H2Test {
private static final String DS_GROUP_NAME = "h2"; private static final String DS_GROUP_NAME = "h2";
@BeforeClass @BeforeAll
public static void init() throws SQLException { public static void init() throws SQLException {
Db db = Db.use(DS_GROUP_NAME); Db db = Db.use(DS_GROUP_NAME);
db.execute("CREATE TABLE test(a INTEGER, b BIGINT)"); db.execute("CREATE TABLE test(a INTEGER, b BIGINT)");

View File

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

View File

@ -1,22 +1,22 @@
package cn.hutool.db; package cn.hutool.db;
import cn.hutool.core.lang.Console; 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.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
* MySQL操作单元测试 * MySQL操作单元测试
* *
* @author looly * @author looly
*/ */
public class MySQLTest { public class MySQLTest {
@BeforeClass //@BeforeAll
@Disabled
public static void createTable() throws SQLException { public static void createTable() throws SQLException {
Db db = Db.use("mysql"); 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"); 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> * 事务测试<br>
* 更新三条信息低2条后抛出异常正常情况下三条都应该不变 * 更新三条信息低2条后抛出异常正常情况下三条都应该不变
*
* @throws SQLException SQL异常
*/ */
@Test(expected = SQLException.class) @Test
@Disabled @Disabled
public void txTest() throws SQLException { public void txTest() {
Db.use("mysql").tx(db -> { assertThrows(SQLException.class, () -> {
int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100)); Db.use("mysql").tx(db -> {
db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101)); int update = db.update(Entity.create("user").set("text", "描述100"), Entity.create().set("id", 100));
if (1 == update) { db.update(Entity.create("user").set("text", "描述101"), Entity.create().set("id", 101));
// 手动指定异常然后测试回滚触发 if (1 == update) {
throw new RuntimeException("Error"); // 手动指定异常然后测试回滚触发
} throw new RuntimeException("Error");
db.update(Entity.create("user").set("text", "描述102"), Entity.create().set("id", 102)); }
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")); db.upsert(Entity.create("testuser").set("id", 1).set("account", "icefairy").set("pass", "a123456"));
Entity user = db.get(Entity.create("testuser").set("id", 1)); Entity user = db.get(Entity.create("testuser").set("id", 1));
System.out.println("user======="+user.getStr("account")+"___"+user.getStr("pass")); 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; package cn.hutool.db;
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.sql.SQLException; import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UpdateTest { public class UpdateTest {
Db db; Db db;
@Before @BeforeEach
public void init() { public void init() {
db = Db.use("test"); 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.core.util.StrUtil;
import cn.hutool.db.DbRuntimeException; import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.ds.DSFactory; import cn.hutool.db.ds.DSFactory;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.List; 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() { public void getTableNotExistTest() {
final Table table = MetaUtil.getTableMeta(ds, "user_not_exist"); assertThrows(DbRuntimeException.class, () -> {
assertEquals(table.getIndexInfoList().size(), 2); final Table table = MetaUtil.getTableMeta(ds, "user_not_exist");
assertEquals(table.getIndexInfoList().size(), 2);
});
} }
} }

View File

@ -1,17 +1,18 @@
package cn.hutool.extra.spring; package cn.hutool.extra.spring;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* @author sidian * @author sidian
*/ */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EnableSpringUtilTest.class) @SpringBootTest(classes = EnableSpringUtilTest.class)
@EnableSpringUtil @EnableSpringUtil
@ExtendWith(SpringExtension.class)
public class EnableSpringUtilTest { public class EnableSpringUtilTest {
@Test @Test

View File

@ -3,22 +3,27 @@ package cn.hutool.extra.spring;
import cn.hutool.core.lang.TypeReference; import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import lombok.Data; import lombok.Data;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class) import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {SpringUtil.class, SpringUtilTest.Demo2.class}) @SpringBootTest(classes = {SpringUtil.class, SpringUtilTest.Demo2.class})
//@Import(cn.hutool.extra.spring.SpringUtil.class) // @ActiveProfiles("dev") // SpringUtil.getActiveProfile()效果与下面方式一致
@TestPropertySource(properties = {"spring.profiles.active=dev"})
//@Import(spring.org.dromara.hutool.extra.SpringUtil.class)
public class SpringUtilTest { public class SpringUtilTest {
/** /**

View File

@ -3,20 +3,21 @@ package cn.hutool.extra.spring;
import cn.hutool.core.lang.TypeReference; import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import lombok.Data; import lombok.Data;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class) import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {SpringUtilWithAutoConfigTest.Demo2.class}) @SpringBootTest(classes = {SpringUtilWithAutoConfigTest.Demo2.class})
//@Import(cn.hutool.extra.spring.SpringUtil.class)
@EnableAutoConfiguration @EnableAutoConfiguration
public class SpringUtilWithAutoConfigTest { public class SpringUtilWithAutoConfigTest {

19
pom.xml
View File

@ -95,7 +95,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.13.0</version>
<configuration> <configuration>
<source>${compile.version}</source> <source>${compile.version}</source>
<target>${compile.version}</target> <target>${compile.version}</target>
@ -105,7 +105,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version> <version>3.4.2</version>
<configuration> <configuration>
<archive> <archive>
<index>true</index> <index>true</index>
@ -126,7 +126,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version> <version>3.8.0</version>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>
@ -136,6 +136,15 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- 统一更新pom版本 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.17.1</version>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
@ -158,7 +167,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version> <version>3.3.1</version>
<executions> <executions>
<execution> <execution>
<id>oss</id> <id>oss</id>
@ -200,7 +209,7 @@
<plugin> <plugin>
<groupId>org.sonatype.plugins</groupId> <groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId> <artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version> <version>1.7.0</version>
<extensions>true</extensions> <extensions>true</extensions>
<configuration> <configuration>
<serverId>oss</serverId> <serverId>oss</serverId>