springboot与单元测试整合

依赖架包

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

在单元测试文件下,测试类上添加注解@SpringBootTest

在这里插入图片描述

@RunWith(SpringRunner.class)
@SpringBootTest   //扫描SpringbootTestApplication启动类,启动类扫描mapper
public class SpringbootTestApplicationTests {
    
     @Autowired
 	UserTestMapper userTestMapper;
     
	@Test
	public void testuserTestMapper() {
		 UserTestCriteria example =new UserTestCriteria();
		 com.testfan.model.UserTestCriteria.Criteria cri = example.createCriteria();
		 cri.andLoginnameLike("z");
		 Criteria cri2= example.createCriteria();
		 cri2.andLoginpassBetween("1", "1000");
		 example.or(cri2);
		userTestMapper.selectByExample(example);
	}
    
}

注意:SpringbootTestApplicationTests 和SpringbootTestApplication需要在同一级目录下,否则扫描不到启动类文件

如何开启sql查询日志

在application.properites文件中添加配置

#对应的包com.testfan.dao 根据实际情况而定
logging.level.com.testfan.dao=debug
SpringBoot 3.0中进行单元测试非常简单。以下是进行单元测试的步骤: 1. 导入必要的依赖。在你的项目的pom.xml文件中,添加以下依赖: ```xml <!--测试类依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 这个依赖将会提供所需的测试框架和工具。 2. 编写单元测试类和测试方法。你可以在测试类中使用`@RunWith(SpringRunner.class)`注解来指定测试运行器,并加载Spring Boot测试注解。使用`@SpringBootTest`注解标记测试类,并加载项目的上下文环境ApplicationContext。例如: ```java package com.xin.spring_01; import com.xin.spring_01.controller.FirstController; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest class Spring01ApplicationTests { @Autowired private FirstController firstController; @Test void contextLoads() { //编写测试方法 //可以使用@Autowired注解注入需要测试的类或组件 //执行测试逻辑 } } ``` 在这个例子中,我们注入了`FirstController`类,并在`contextLoads`方法中编写了测试逻辑。 通过以上步骤,你就可以使用SpringBoot 3.0进行单元测试了。记得在测试方法中编写适当的测试逻辑,以确保你的代码能够正确运行和通过测试。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合Junit](https://blog.csdn.net/JHIII/article/details/126303440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot(3)单元测试与热部署](https://blog.csdn.net/qq_42743832/article/details/120026074)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值