久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:77日期: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整合:

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

1、導入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(指定配置文件)

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 黄平县| 桐柏县| 息烽县| 江永县| 扎兰屯市| 通榆县| 托克托县| 邯郸市| 手机| 潢川县| 巩义市| 乌拉特中旗| 康保县| 天津市| 丽水市| 柳州市| 梨树县| 浠水县| 吴旗县| 临漳县| 乐陵市| 宕昌县| 乌拉特前旗| 上蔡县| 红安县| 稻城县| 拉萨市| 花莲市| 娱乐| 东乌| 北碚区| 湘潭县| 昌图县| 铜山县| 山西省| 德兴市| 巴彦淖尔市| 桓仁| 长沙市| 伊宁县| 荣成市|