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://www.toymoban.com/news/detail-797076.html
文章来源:https://www.toymoban.com/news/detail-797076.html
到了这里,关于Spring Boot整合Junit的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!