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

相關文章
相關標籤/搜索