国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Junit寫法及與spring整合過程詳解

瀏覽:53日期:2023-09-01 15:34:53

junit之前的寫法:

//在Before中注入service類private IUserService userService; @Beforepublic void setUp() throws Exception {//使用xml的方式ApplicationContext applicationContext = new ClassPathXmlApplicationContext('applicationContext.xml');//使用注解的方式ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);userService = applicationContext.getBean(IUserService.class); }

Spring與junit整合:

不需要手動創(chuàng)建Spring容器, 自動把bean注入到測試類

1、導(dǎo)入spring-test的依賴, 需要junit

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.3.RELEASE</version></dependency>

2、在測試類使用spring-test的注解

@RunWith(class)

@ContextConfiguration(指定配置文件)

//測試類運行的環(huán)境,在spring環(huán)境下運行,在測試類, 注入Spring容器的bean@RunWith(SpringJUnit4ClassRunner.class)//在創(chuàng)建spring容器時,指定加載哪個配置文件 - - 相當(dāng)于之前的手動獲取對象@ContextConfiguration('classpath:applicationContext.xml')public class UserServiceImplTest { @Autowired //注入IUserService private IUserService userService; @Test public void testFindUserById() { userService.findUserById(2); }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 双峰县| 平安县| 大连市| 怀安县| 沾化县| 开封县| 三明市| 乡宁县| 讷河市| 房山区| 铜鼓县| 白城市| 永年县| 汝城县| 乐陵市| 晴隆县| 南和县| 安乡县| 天水市| 鄂托克旗| 柯坪县| 无棣县| 泰安市| 札达县| 贡嘎县| 定州市| 贡嘎县| 罗平县| 宜黄县| 胶南市| 乌拉特前旗| 宜昌市| 新绛县| 聂荣县| 厦门市| 海南省| 揭西县| 元谋县| 新巴尔虎右旗| 嵩明县| 铁岭市|