Spring Boot整合Junit

Spring Boot整合Junit

Junit启动器

xml 复制代码
		<!--junit启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

编写业务代码

dao

java 复制代码
@Repository
public class UserDaoImpl {

	public void saveUser(){
		System.out.println("insert into users.....");
	}
}

service

java 复制代码
@Service
public class UserServiceImpl {

	@Autowired
	private UserDaoImpl userDaoImpl;
	
	public void addUser(){
		this.userDaoImpl.saveUser();
	}
}

app

java 复制代码
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

整合Junit

java 复制代码
/**
 *  main方法:
 *		ApplicationContext ac=new 
 *       			ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 *  junit与spring整合:
 *      @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *   	@Contextconfiguartion("classpath:applicationContext.xml")  
 */
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes={App.class})
public class UserServiceTest {

	@Autowired
	private UserServiceImpl userServiceImpl;
	
	@Test
	public void testAddUser(){
		this.userServiceImpl.addUser();
	}
}
相关推荐
小马哥编程3 分钟前
【软考架构】滑动窗口限流算法的原理是什么?
java·开发语言·架构
疯狂的程序猴6 分钟前
IPA 深度混淆是什么意思?分析其与普通混淆的区别
后端
cci9 分钟前
Remote ssh无法连接?
后端
云栖梦泽11 分钟前
鸿蒙数据持久化实战:构建本地存储与云同步系统
开发语言·鸿蒙系统
wjs202417 分钟前
《Ionic 侧栏菜单》
开发语言
祁思妙想18 分钟前
linux常用命令
开发语言·python
JohnYan18 分钟前
Bun技术评估 - 22 Stream
javascript·后端·bun
饕餮争锋22 分钟前
Spring AOP原理简析
java·spring
okseekw31 分钟前
Maven从入门到实战:核心概念+配置详解+避坑指南
java·后端
该用户已不存在32 分钟前
Node.js后端开发必不可少的7个核心库
javascript·后端·node.js