|
1 | 1 | package cn.wildfirechat.app; |
2 | 2 |
|
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; |
6 | 4 | import org.junit.Test; |
7 | 5 | import org.junit.runner.RunWith; |
8 | | -import org.springframework.beans.factory.annotation.Autowired; |
9 | 6 | 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; |
13 | 10 | import org.springframework.test.context.junit4.SpringRunner; |
14 | 11 |
|
15 | | -import javax.persistence.EntityManagerFactory; |
| 12 | +import javax.sql.DataSource; |
16 | 13 |
|
17 | 14 | import static org.junit.Assert.assertNotNull; |
18 | 15 |
|
| 16 | +/** |
| 17 | + * Application Tests |
| 18 | + * |
| 19 | + * 数据库配置说明: |
| 20 | + * - 默认使用 H2 内存数据库(无需外部数据库即可运行测试) |
| 21 | + * - 如需测试 MySQL,启用 MySqlTestConfig |
| 22 | + * - 如需测试达梦数据库,启用 DamengTestConfig |
| 23 | + * |
| 24 | + * 启用方式:将对应配置类的 @Primary 注解取消注释即可 |
| 25 | + */ |
19 | 26 | @RunWith(SpringRunner.class) |
20 | 27 | @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 | | -}) |
30 | 28 | public class ApplicationTests { |
31 | 29 |
|
32 | 30 | @Test |
33 | 31 | public void contextLoads() { |
34 | 32 | assertNotNull("Application should load", true); |
35 | 33 | } |
36 | 34 |
|
| 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 | + |
37 | 92 | } |
0 commit comments