自学内容网 自学内容网

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


原文地址:https://blog.csdn.net/qq_62965575/article/details/135634972

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!