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();
    }
}
相关推荐
武斌34 分钟前
需要独立的作业队列?看看Quartz增强框架Quartz Plus
java·spring boot·后端
while(1){yan}40 分钟前
Mybatis动态SQL
数据库·spring boot·sql·spring·java-ee·mybatis
计算机毕设指导61 小时前
基于微信小程序的扶贫助农系统【源码文末联系】
java·spring boot·后端·微信小程序·小程序·tomcat·maven
魏波.1 小时前
SpringBoot中Profile 多环境配置(dev/test/prod)详解
spring boot
jyd01242 小时前
Spring Boot 文件上传大小限制问题:413 与 MaxUploadSizeExceededException 解决方案
java·spring boot·后端
虾说羊2 小时前
Spring Boot前后端分离项目部署
java·spring boot·后端
vx_bisheyuange3 小时前
基于SpringBoot的青年公寓服务平台
前端·vue.js·spring boot·毕业设计
小王不爱笑1323 小时前
SpringBoot 项目新建的五种方式详细笔记
spring boot·笔记·后端
悟空码字3 小时前
SpringBoot整合FFmpeg,打造你的专属视频处理工厂
java·spring boot·后端
独自归家的兔3 小时前
Spring Boot 版本怎么选?2/3/4 深度对比 + 迁移避坑指南(含 Java 8→21 适配要点)
java·spring boot·后端