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();
	}
}
相关推荐
栀寒老醑1 小时前
Python实现的服务器日志监控脚本
开发语言·python
星星点点洲1 小时前
PostgreSQL 15二进制文件
开发语言·设计模式·golang
小糖学代码1 小时前
Linux:11.线程概念与控制
linux·服务器·c语言·开发语言·c++
yaoxin5211232 小时前
211. Java 异常 - Java 异常机制总结
java·开发语言·python
Empty_7774 小时前
编程之python基础
开发语言·python
疯狂吧小飞牛4 小时前
Lua 中的 __index、__newindex、rawget 与 rawset 介绍
开发语言·junit·lua
寻星探路6 小时前
Java EE初阶启程记13---JUC(java.util.concurrent) 的常见类
java·开发语言·java-ee
哲Zheᗜe༘7 小时前
了解学习Python编程之python基础
开发语言·python·学习
落日漫游7 小时前
数据结构笔试核心考点
java·开发语言·算法
寻找华年的锦瑟8 小时前
Qt-配置文件(INI/JSON/XML)
开发语言·qt