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();
    }
}
相关推荐
看世界的小gui3 分钟前
Jeecgboot通过Maxkey实现单点登录完整方案
java·spring boot·jeecgboot
10km8 分钟前
Spring Boot 环境下使用 Map<String, MultipartFile> 实现文件上传功能
java·spring boot·mock·map·multipartfile
yangminlei26 分钟前
使用 Cursor 快速创建一个springboot项目
spring boot·ai编程
tb_first43 分钟前
万字超详细苍穹外卖学习笔记1
java·jvm·spring boot·笔记·学习·tomcat·mybatis
今晚打老虎z1 小时前
解决SQL Server 安装运行时针对宿主机内存不足2GB的场景
sqlserver·c#
东东5162 小时前
校园求职招聘系统设计和实现 springboot +vue
java·vue.js·spring boot·求职招聘·毕设
弹简特4 小时前
【JavaEE05-后端部分】使用idea社区版从零开始创建第一个 SpringBoot 程序
java·spring boot·后端
Demon_Hao5 小时前
Spring Boot开启虚拟线程ScopedValue上下文传递
java·spring boot·后端
半聋半瞎5 小时前
Flowable快速入门(Spring Boot整合版)
java·spring boot·后端·flowable
毕设源码-邱学长5 小时前
【开题答辩全过程】以 基于SpringBoot的理工学院学术档案管理系统为例,包含答辩的问题和答案
java·spring boot·后端