Cannot resolve symbol 'SpringJUnit4ClassRunner'

ps:idea打开以后,发现test类报错:Cannot resolve symbol 'SpringJUnit4ClassRunner',注解所有爆红java

package com.it;

import com.it.config.SpringConfiguration;
import com.it.entity.Student;
import com.it.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
/**
 * **
 *  * 使用Junit单元测试:测试咱们的配置
 *  * Spring整合junit的配置
 *  *      一、导入spring整合junit的jar:spring-test(坐标)
 *  *      二、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的
 *  *             @Runwith
 *  *      三、告知spring的运行器,spring和ioc建立是基于xml仍是注解的,而且说明位置
 *  *          @ContextConfiguration
 *  *                  locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
 *  *                  classes:指定注解类所在地位置
 *  *
 *  *   当咱们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
 *  */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)

public class TestClient {
    @Autowired
    private  StudentService as;
    @Test
    public  void findAll(){
        List<Student> studentList =as.findAllStudent();
        for (Student student:studentList) {
            System.out.println(student);
        }
    }

    @Test
    public  void findbyidtest() {
        //ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Student student = as.findByid(4);
        System.out.println(student);
    }

    @Test
    public  void saveTest() {
        Student student=new Student();
        student.setId(7);
        student.setStuno("10007");
        student.setName("陈多糖");
        student.setClassid(2);
        as.saveStudent(student);
    }
    @Test
    public  void updatetest() {
        Student student = as.findByid(4);
        student.setName("陈柳柳");
        as.updateStudent(student);
    }

    @Test
    public  void deletetest() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        StudentService as = ac.getBean("studentService", StudentService.class);
        as.deleteStudent(7);
    }
}

去掉<scope>test</scope>,去掉以后就能够运行成功可是这个是什么缘由啦??咱们的先了解一下pom依赖中<scope>????</scope>的含义是什么spring

在一个maven项目中,若是存在编译须要而发布不须要的jar包,能够用scope标签,值设为provided。以下:api

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
            <classifier />
        </dependency>jsp

scope的其余参数以下:maven

    • compile
      默认的scope,表示 dependency 均可以在生命周期中使用。并且,这些dependencies 会传递到依赖的项目中。适用于全部阶段,会随着项目一块儿发布
    • provided
      跟compile类似,可是代表了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能做用在编译和测试时,同时没有传递性。????????
    • runtime
      表示dependency不做用在编译时,但会做用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
    • test
      表示dependency做用在测试时,不做用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。
    • system
      跟provided 类似,可是在系统中要之外部JAR包的形式提供,maven不会在repository查找它。

 这个问题其实你由于你不熟悉maven文件结构所致.测试类通常是放在src/test/java,而不是放在src/main/java下.maven在编译的时候,src/main/java下是不引用<scope>test</scope>的jar,而编译src/test/java下的测试这会引用<scope>test</scope>的jar,缘由可能就是当使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @Runwith的后,<scope>????<scope>这个时候里面的值就不用test了,test是在测试的时候才起做用,不测试的是不起做用的,要是有的话,这个时候就会找不到SpringJUnitClassRunner.classide

相关文章
相关标签/搜索