Spring Boot整合Junit

发布时间:2024年01月16日

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

文章来源:https://blog.csdn.net/qq_62965575/article/details/135634972
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。