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