Spring整合junit的配置

所需jar依賴

<!--spring ioc-->
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <!--導入spring整合junit jar-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.0.2.RELEASE</version>
          <scope>test</scope>
      </dependency>
    <!--測試類-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

以下是一個測試類事例
當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上的版本

/**
 * Spring整合junit的配置
 *      1、導入spring整合junit的jar(座標)
 *      2、使用Junit提供的一個註解把原有的main方法替換了,替換成spring提供的
 *             @Runwith
 *      3、告知spring的運行器,spring和ioc創建是基於xml還是註解的,並且說明位置
 *          @ContextConfiguration
 *                  locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下
 *                  classes:指定註解類所在地位置
 *
 *   
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Demo {
//爲u注入UserServiceDaoImpl對象
    @Autowired
private UserServiceDaoImpl u;

    @Test
    public void test01(){
		UserServiceDaoImpl.class);
       System.out.println(u.findAllUser());
    }
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章