① 导入spring集成Junit的坐标(在pom.xml改)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.新创建一个Test类
package com.itcast;
import cn.itcast.cofig.SpringCofiguration;
import cn.itcast.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author QLBF
* @version 1.0
* @date 2021/1/12 14:18
*/
@RunWith(SpringJUnit4ClassRunner.class) //SpringJUnit4ClassRunner是java提供的,不可改
//@ContextConfiguration("classpath:applicationContext.xml")//用原始注解也可以
@ContextConfiguration(classes = {SpringCofiguration.class})//也可以使用新注解,{}可以写多个新注解类
public class Springjunittest {
@Autowired //下面写你要使用的对象
private UserService userService;
@Test
public void test1()
{
userService.save();
}
}
UserDao等参考上一篇博客的,这里就不写了
点Test1左边小绿点就可以,执行成功