Skip to content

Commit a8d749f

Browse files
author
heavyrian2012
committed
测试代码
1 parent bb6caaa commit a8d749f

2 files changed

Lines changed: 72 additions & 40 deletions

File tree

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,92 @@
11
package cn.wildfirechat.app;
22

3-
import cn.wildfirechat.app.jpa.*;
4-
import cn.wildfirechat.app.slide.SlideVerifyCleanupService;
5-
import cn.wildfirechat.app.slide.SlideVerifyService;
3+
import com.zaxxer.hikari.HikariDataSource;
64
import org.junit.Test;
75
import org.junit.runner.RunWith;
8-
import org.springframework.beans.factory.annotation.Autowired;
96
import org.springframework.boot.test.context.SpringBootTest;
10-
import org.springframework.boot.test.mock.mockito.MockBean;
11-
import org.springframework.data.jpa.repository.JpaRepository;
12-
import org.springframework.test.context.TestPropertySource;
7+
import org.springframework.boot.test.context.TestConfiguration;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Primary;
1310
import org.springframework.test.context.junit4.SpringRunner;
1411

15-
import javax.persistence.EntityManagerFactory;
12+
import javax.sql.DataSource;
1613

1714
import static org.junit.Assert.assertNotNull;
1815

16+
/**
17+
* Application Tests
18+
*
19+
* 数据库配置说明:
20+
* - 默认使用 H2 内存数据库(无需外部数据库即可运行测试)
21+
* - 如需测试 MySQL,启用 MySqlTestConfig
22+
* - 如需测试达梦数据库,启用 DamengTestConfig
23+
*
24+
* 启用方式:将对应配置类的 @Primary 注解取消注释即可
25+
*/
1926
@RunWith(SpringRunner.class)
2027
@SpringBootTest
21-
@TestPropertySource(properties = {
22-
"spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MySQL",
23-
"spring.datasource.driver-class-name=org.h2.Driver",
24-
"spring.datasource.username=sa",
25-
"spring.datasource.password=",
26-
"spring.jpa.hibernate.ddl-auto=create-drop",
27-
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
28-
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration"
29-
})
3028
public class ApplicationTests {
3129

3230
@Test
3331
public void contextLoads() {
3432
assertNotNull("Application should load", true);
3533
}
3634

35+
// ==================== H2 内存数据库配置(默认) ====================
36+
@TestConfiguration
37+
static class H2TestConfig {
38+
@Bean
39+
@Primary
40+
public DataSource dataSource() {
41+
HikariDataSource dataSource = new HikariDataSource();
42+
dataSource.setDriverClassName("org.h2.Driver");
43+
dataSource.setJdbcUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MySQL");
44+
dataSource.setUsername("sa");
45+
dataSource.setPassword("");
46+
dataSource.setMinimumIdle(1);
47+
dataSource.setMaximumPoolSize(5);
48+
return dataSource;
49+
}
50+
}
51+
52+
// ==================== MySQL 数据库配置示例 ====================
53+
// 使用方法:取消下方 @Primary 注解的注释,并将 H2TestConfig 的 @Primary 注释掉
54+
/*
55+
@TestConfiguration
56+
static class MySqlTestConfig {
57+
@Bean
58+
@Primary
59+
public DataSource dataSource() {
60+
HikariDataSource dataSource = new HikariDataSource();
61+
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
62+
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/appdata?serverTimezone=UTC&useSSL=false");
63+
dataSource.setUsername("root");
64+
dataSource.setPassword("123456");
65+
dataSource.setMinimumIdle(1);
66+
dataSource.setMaximumPoolSize(10);
67+
return dataSource;
68+
}
69+
}
70+
*/
71+
72+
// ==================== 达梦数据库配置示例 ====================
73+
// 使用方法:取消下方 @Primary 注解的注释,并将 H2TestConfig 的 @Primary 注释掉
74+
/*
75+
@TestConfiguration
76+
static class DamengTestConfig {
77+
@Bean
78+
@Primary
79+
public DataSource dataSource() {
80+
HikariDataSource dataSource = new HikariDataSource();
81+
dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
82+
dataSource.setJdbcUrl("jdbc:dm://192.168.1.6:5237");
83+
dataSource.setUsername("SYSDBA");
84+
dataSource.setPassword("Wfc123!@");
85+
dataSource.setMinimumIdle(1);
86+
dataSource.setMaximumPoolSize(5);
87+
return dataSource;
88+
}
89+
}
90+
*/
91+
3792
}

src/test/resources/application.properties

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)