Spring之整合Junit

Spring整合Junit

package com.lyl;

import com.lyl.config.SpringConfig;
import com.lyl.entity.Account;
import com.lyl.service.AccountService;
import org.junit.Before;
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.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * spring整合junit
 *  1.導入spring整合junit的jar
 *  <dependency>
 *             <groupId>org.springframework</groupId>
 *             <artifactId>spring-test</artifactId>
 *             <version>5.0.2.RELEASE</version>
 *  </dependency>
 *  2.使用junit提供的一個註解把原有的main方法替換了,替換成spring提供的,因爲junit的test方法是不會自動注入的
 * @ Runwith
 *  3.告知spring的運行器,spring和ioc創建是基於xml還是基於註解
 * @ ContextConfiguration
 *      location:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下
 *      classes:指定註解類所在的位置
 *  當我們使用spring 5.x版本的時候要求junit的jar必須是4.12以上
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class test {
    /**節省這一段
     * ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
     *
     * AccountService as = (AccountService) ac.getBean("accountService");
     */
    @Autowired
    private AccountService as;
    /*@Before
    public void init(){
        ac=new ClassPathXmlApplicationContext("bean.xml");
        as =(AccountService) ac.getBean("accountService");
    }*/
    @Test
    public void findAll() {
        List<Account> accounts=as.findAllAccount();
        for (Account account:accounts)
            System.out.println(account);
    }
    @Test
    public void testUpdate() {

    }
    @Test
    public void testFindOne() {
        //ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        //註解應該使用AnnotationConfigApplicationContext這個實現類,傳入配置類的字節碼文件
        ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService as =(AccountService) ac.getBean("accountService");
        Account account=as.findAccountById(1);
        System.out.println(account);
    }
    @Test
    public void testSave() {

    }
    @Test
    public void findDelete() {

    }
}

當我們想在test下去使用註解@Autowired去給我們注入我們要測試的類的時候,我們就需要進行如下步驟,否則是不會注入bean到spring容器的(原理,略)

  • 1.導入spring整合junit的jar
    這個jar下面有下面講的Runwith註解的值的一個字節碼文件
    SpringJUnit4ClassRunner.class
    這個jar版本要高一點,否則沒有這個字節碼文件
  <dependency>
             <groupId>org.springframework</groupId>
              <artifactId>spring-test</artifactId>
              <version>5.0.2.RELEASE</version>
  </dependency>
  • 2.使用junit提供的一個註解把原有的main方法替換了,替換成spring提供的,因爲junit的test方法是不會將bean自動注入的
    @ Runwith
    屬性值填這個 SpringJUnit4ClassRunner.class
  • 3.告知spring的運行器,spring和ioc創建是基於xml還是基於註解
    @ContextConfiguration
    (基於XML) location:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下
    (基於註解)classes:指定註解類所在的位置
    當我們使用spring 5.x版本的時候要求junit的jar必須是4.12以上,否則報錯
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章