spring集成junit

1 导入坐标

2使用@Runwith来替换原来的运行期

3使用@ContextConfiguration指定配置文件或者配置类

4注入需要测试的对象

5 测试

集成前

    @Test
    public void test6() throws Exception{

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringCofiguration.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.save();


    }

集成后

import com.jc.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.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.xml.ws.Service;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//指定文件或类
//@ContextConfiguration("SpringCofiguration.class")//指定文件或类
public class SpringJunitTest {
    @Autowired
    private UserService userService;

    @Test
    public void test(){
        userService.save();
    }
}
原文地址:https://www.cnblogs.com/ziwang520/p/15797912.html