单元测试mysql切换到h2_java~springboot~h2数据库在单元测试中的使用

本文介绍了如何在SpringBoot项目中进行单元测试时,将数据库从MySQL切换到内存数据库H2。强调了单元测试的原则,如避免依赖和外部资源,以及如何配置和使用H2数据库进行集成测试。通过示例展示了测试基类和具体控制器测试类的编写,并提供了相关依赖和H2数据库的配置详情。
摘要由CSDN通过智能技术生成

单元测试有几点要说的

事实上springboot框架是一个tdd框架,你在进行建立项目时它会同时建立一个单元测试项目,而我们的代码用例可以在这个项目里完成,对于单元测试大叔有以下几点需要说明一下:

单元测试的用例之间不要有相互依赖

单元测试数据来源为本地,不要访问外部资源,外部数据库也是不行的

对于集成测试,每个控制器对应一个测试类,它们可以有统一的抽象基类,用来存储公用的属性,如数据对象,http对象等

引入相关依赖包

testCompile('com.h2database:h2')

下面对h2数据库的配置

spring:

profiles: integTest

cloud.config.enabled: false

h2:

console:

enabled: true

path: /h2

datasource:

url: jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false

driver-class-name: org.h2.Driver

username: sa

password: sa

schema-username: sa

schema-password: sa

data-username: sa

data-password: sa

schema: classpath:db/*.sql

data: classpath:data/*.sql

initialization-mode: always

platform: h2

下面直接写单元测试即可,业务层不用修改,数据库根据profile去选择mysql还是h2

@RunWith(SpringRunner.class)

@ActiveProfiles("integTest")

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public abstract class ControllerTestBase {

protected MockMvc mockMvc;

@Autowired

protected ObjectMapper objectMapper;

@Autowired

private WebApplicationContext webApplicationContext;

@Before

public void setUp() {

mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

}

public class CustomerTagsControllerTest extends ControllerTestBase {

@Autowired

CustomerTagsService customerTagsService;

@Test

public void getCustomerTags() throws Exception {

mockMvc.perform(

get("/api/tags")

.accept(MediaType.APPLICATION_JSON_UTF8))

.andExpect(status().isOk())

.andExpect(jsonPath("$[0].tagsDescription").value("未接"));

}

}

对于h2在单元测试里的使用就说这么说,有不清楚的可以给大叔留言!

Spring Boot 内嵌的 H2 数据库是一款基于 Java 的内存数据库,可以很方便地在应用程序使用使用 H2 数据库有很多好处,比如它非常轻量级,可以快速启动和停止,适合于开发和测试环境等。 下面是在 Spring Boot 使用 H2 数据库的步骤: 1. 在 pom.xml 添加 H2 数据库的依赖: ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 2. 在 application.properties 文件配置 H2 数据库相关信息: ```properties # H2 Database spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= ``` 这里的配置将 H2 数据库的 URL 设置为 `jdbc:h2:mem:testdb`,表示使用内存数据库,用户名和密码分别设置为 `sa` 和空字符串。 3. 创建实体类和数据访问层: ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; // getters and setters } @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 这里我们创建了一个名为 `User` 的实体类,并使用 `JpaRepository` 接口来访问数据库。 4. 创建控制器和服务层: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public List<User> getUsers() { return userService.getUsers(); } } @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getUsers() { return userRepository.findAll(); } } ``` 这里我们创建了一个名为 `UserController` 的控制器,并使用 `UserService` 来获取用户列表。 5. 运行应用程序,并访问 http://localhost:8080/users 即可查看用户列表。 总之,使用 H2 数据库内嵌到 Spring Boot 应用程序非常简单,可以方便地进行开发和测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值