Java Config下的Spring Test幾種方式實(shí)例詳解
Java Config 下的Spring Test方式
用了三種方式:
1.純手動(dòng)取bean:
package com.wang.test;
import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Wanglei on 15/10/29.
*/
public class CustomeTest {
private static AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before
public void tearUp(){
context.register(PropertyConfig.class);
context.register(ServiceConfig.class);
context.register(SecurityConfig.class);
context.register(MapperConfig.class);
context.refresh();
}
@Test
public void testUser(){
UserService userService = context.getBean(UserService.class);
Long userId = 3L;
GeneralResponseData data = userService.addUserRelation(userId);
System.out.println(data.getMsg());
}
}
2.采用spring-test
package com.wang.test;
import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by Wanglei on 15/10/29.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PropertyConfig.class, ServiceConfig.class, SecurityConfig.class, MapperConfig.class})
public class SpringTest {
@Autowired
private UserService userService;
@Test
public void testUser(){
GeneralResponseData data= userService.addUserRelation(3L);
System.out.println(data.getMsg());
}
}
3.采用Mockito
需要引入相應(yīng)包:
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency>
package com.wang.test;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.presistence.FollowNumberMapper;
import com.marsmother.commission.core.presistence.UserMapper;
import com.marsmother.commission.core.presistence.UserRelationMapper;
import com.marsmother.commission.core.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Created by Wanglei on 15/10/29.
*/
public class TestUserService {
@InjectMocks
private UserService userService;
@Mock
private FollowNumberMapper followNumberMapper;
@Mock
private UserMapper userMapper;
@Mock
private UserRelationMapper userRelationMapper;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testUser(){
Long userId = 3L;
GeneralResponseData result = userService.addUserRelation(userId);
System.out.println(result.getMsg());
}
}
這里@Mock的話(huà),并不會(huì)真正的去執(zhí)行數(shù)據(jù)庫(kù)的操作。
還有一種用法是@Spy,暫時(shí)不了解具體使用方式,待研究。
相比之下,還是spring-test標(biāo)準(zhǔn)一些。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- JavaEE開(kāi)發(fā)之SpringMVC中的自定義消息轉(zhuǎn)換器與文件上傳
- Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
- Spring 4 支持的 Java 8 特性
- Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶(hù)的實(shí)現(xiàn)方法
- java web SpringMVC后端傳json數(shù)據(jù)到前端頁(yè)面實(shí)例代碼
- 詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊(cè)到Spring中
- java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法
- Java中spring讀取配置文件的幾種方法示例
相關(guān)文章
淺析Java中關(guān)鍵詞volatile底層的實(shí)現(xiàn)原理
在 Java 并發(fā)編程中,有 3 個(gè)最常用的關(guān)鍵字:synchronized、ReentrantLock 和 volatile,這篇文章主要來(lái)和大家聊聊volatile底層的實(shí)現(xiàn)原理,感興趣的可以了解下2024-02-02
Java實(shí)現(xiàn)斗地主最簡(jiǎn)代碼實(shí)例
在本篇文章里小編給各位分享的是關(guān)于Java實(shí)現(xiàn)斗地主最簡(jiǎn)代碼實(shí)例,有興趣的朋友們可以參考下。2020-05-05
java實(shí)現(xiàn)馬踏棋盤(pán)的完整版
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤(pán)的完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Elasticsearch模糊查詢(xún)?cè)敿?xì)介紹
這篇文章主要給大家介紹了關(guān)于Elasticsearch模糊查詢(xún)的相關(guān)資料,在數(shù)據(jù)庫(kù)查詢(xún)中模糊查詢(xún)是一種強(qiáng)大的技術(shù),可以用來(lái)搜索與指定模式匹配的數(shù)據(jù),需要的朋友可以參考下2023-09-09
使用Vert.x Maven插件快速創(chuàng)建項(xiàng)目的方法
這篇文章主要介紹了使用Vert.x Maven插件快速創(chuàng)建項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Java StringBuilder類(lèi)原理及常用方法
這篇文章主要介紹了Java StringBuilder類(lèi)原理及常用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式
這篇文章主要介紹了如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
SpringCloud Gateway的路由,過(guò)濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過(guò)濾器和限流解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例
這篇文章主要給大家介紹了關(guān)于java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例
這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例,從創(chuàng)建一個(gè)web項(xiàng)目開(kāi)始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁(yè)面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12

