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
這個問題其實你由於你不熟悉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