Spring Boot整合Junit

1.Junit启动器,配置pox.xml

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

2.编写业务代码

2.1dao

java 复制代码
package com.zhy.dao;


import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void addUser() {
        System.out.println("insert into User .......");
    }
}

2.2service

java 复制代码
package com.zhy.service;

import com.zhy.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public void addUser() {
        userDao.addUser();
    }
}

2.3编写启动类SpringbootJunitApplication

java 复制代码
package com.zhy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

2.4整合Junit

java 复制代码
package com.zhy.test;

import com.zhy.SpringbootJunitApplication;
import com.zhy.service.UserService;
import org.junit.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(classes = SpringbootJunitApplication.class)
public class test {

    @Autowired
    private UserService userService;

    @Test
    public  void  testUser(){
        userService.addUser();
    }
}
相关推荐
jameslogo5 小时前
如何用RocketMQTemplate发送事务消息
java·spring boot·rocketmq
无关86887 小时前
Spring Boot 项目标准化部署打包实战
java·spring boot·后端
jay神7 小时前
基于微信小程序课外创新实践学分认定系统
java·spring boot·小程序·vue·毕业设计
阿丰资源8 小时前
基于Spring Boot的酒店客房管理系统
java·spring boot·后端
zzqssliu8 小时前
SpringBoot框架搭建跨境独立站|Taocarts代购系统订单模块深度开发
java·spring boot·后端
武子康8 小时前
Java-219 RocketMQ Spring Boot 集成指南:生产者与消费者实战
java·spring boot·分布式·kafka·消息队列·rocketmq·java-rocketmq
想学习java初学者9 小时前
SpringBoot整合GS1编码解码
java·spring boot·后端
i220818 Faiz Ul10 小时前
智慧养老平台|基于SprinBoot+vue的智慧养老平台系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·智慧养老平台
Flittly11 小时前
【日常小问】Spring Cloud Gateway 5.x 跨域和路由配置踩坑实录
java·spring boot·spring cloud