国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

junit4 單元測試

2019-11-14 21:03:40
字體:
供稿:網(wǎng)友
junit4 單元測試

1.所需jar包

下載地址:http://search.maven.org/

搜索junit-4.12-beta-1

  aspectjweaver-1.8.2

  sPRing-test-4.0.6.RELEASE

2.3中測試形式

(1)最古老的

package sy.test;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;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 sy.model.User;import sy.service.IUserService;//@RunWith(SpringJUnit4ClassRunner.class)//@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})public class TestMybatis {        //使用test注解    @Test    public void test1(){        //讀取配置文件,如有多個,用逗號隔開        ApplicationContext ctf =                 new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});        //獲取userService實例        IUserService userService = (IUserService) ctf.getBean("userService");        User user = userService.getUserById("0");        System.out.println(user);    }    }

  (2).如果有多個test,則spring每次都要去加載讀取上下文,很浪費資源,因此

package sy.test;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;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 sy.model.User;import sy.service.IUserService;//@RunWith(SpringJUnit4ClassRunner.class)//@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})public class TestMybatis {        private IUserService userService;    private ApplicationContext ctf;    @Before    public void before(){        ctf = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});         userService = (IUserService) ctf.getBean("userService");    }        //使用test注解    @Test    public void test1(){        User user = userService.getUserById("0");        System.out.println(user);    }        //使用autowired注解    @Autowired    public void setUserService(IUserService userService) {        this.userService = userService;    }        }

@Before注解會在所有test測試之前執(zhí)行

(3)spring與junit整合

package sy.test;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;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 sy.model.User;import sy.service.IUserService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})public class TestMybatis {        private IUserService userService;        //使用test注解    @Test    public void test1(){        User user = userService.getUserById("0");        System.out.println(user);    }        //使用autowired注解    @Autowired    public void setUserService(IUserService userService) {        this.userService = userService;    }        }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 将乐县| 格尔木市| 刚察县| 梅州市| 桐乡市| 临夏县| 龙川县| 类乌齐县| 鞍山市| 广宁县| 平罗县| 华坪县| 肥东县| 梓潼县| 宁陵县| 桦甸市| 女性| 鹿邑县| 家居| 衡南县| 金山区| 镇康县| 定日县| 浦东新区| 连江县| 乃东县| 甘洛县| 巴林右旗| 邵武市| 浮梁县| 高安市| 广饶县| 台前县| 石渠县| 青浦区| 江安县| 汉中市| 宁城县| 青冈县| 中方县| 彭山县|