Spring Boot整合Junit

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

<!--junit启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

2.编写业务代码

2.1dao

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

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

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

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();
    }
}

相关推荐

  1. Spring Boot整合Junit

    2024-01-20 09:06:03       46 阅读
  2. Spring Boot整合Junit

    2024-01-20 09:06:03       56 阅读
  3. Spring Boot整合Junit

    2024-01-20 09:06:03       56 阅读
  4. Spring整合Junit4

    2024-01-20 09:06:03       50 阅读
  5. Spring 整合 MyBatis、Junit

    2024-01-20 09:06:03       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 09:06:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 09:06:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 09:06:03       82 阅读
  4. Python语言-面向对象

    2024-01-20 09:06:03       91 阅读

热门阅读

  1. [算法与数据结构]:LRU Cache 的原理与C++实现

    2024-01-20 09:06:03       58 阅读
  2. Linux 命令:grep

    2024-01-20 09:06:03       57 阅读
  3. 高并发服务器 poll模型 非阻塞 讲解

    2024-01-20 09:06:03       51 阅读
  4. 【Linux】01 Ubantu安装NFS服务器及其使用

    2024-01-20 09:06:03       56 阅读
  5. 算法训练营Day44

    2024-01-20 09:06:03       49 阅读
  6. 【C#】Int32.Parse()、Int16.Parse()

    2024-01-20 09:06:03       47 阅读
  7. 数据备份与恢复

    2024-01-20 09:06:03       40 阅读
  8. Spring最常用组件注册注解开发案例

    2024-01-20 09:06:03       45 阅读
  9. ABAP - 变量杂例1

    2024-01-20 09:06:03       47 阅读