Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
order-group
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
order-group-application
order-group
Commits
b778b8ff
Commit
b778b8ff
authored
Mar 10, 2022
by
周晓航
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化购物车结算项目的spock
parent
7554fbee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
14 deletions
+47
-14
shopping-cart-application-service/src/test/spock/cn/freemud/adapter/DemoSpockMock.groovy
+14
-4
shopping-cart-application-service/src/test/spock/cn/freemud/service/impl/FishermanSpock.groovy
+33
-10
No files found.
shopping-cart-application-service/src/test/spock/cn/freemud/adapter/DemoSpockMock.groovy
View file @
b778b8ff
package
cn.freemud.adapter
package
cn.freemud.adapter
import
cn.freemud.adapter.ActivityAdapter
import
cn.freemud.adapter.ActivityAdapter
import
cn.freemud.entities.vo.GetShoppingCartGoodsApportionRequestVo
import
cn.freemud.service.impl.ShoppingCartNewServiceImpl
import
cn.freemud.service.impl.ShoppingCartNewServiceImpl
import
com.alibaba.fastjson.JSON
import
com.alibaba.fastjson.JSON
import
com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import
com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import
com.freemud.api.assortment.datamanager.manager.customer.AssortmentCustomerInfoManager
import
org.junit.runner.RunWith
import
org.junit.runner.RunWith
import
org.mockito.Mockito
import
org.powermock.api.mockito.PowerMockito
import
org.powermock.core.classloader.annotations.PrepareForTest
import
org.powermock.modules.junit4.PowerMockRunner
import
org.powermock.modules.junit4.PowerMockRunner
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
import
org.powermock.modules.junit4.PowerMockRunnerDelegate
import
org.spockframework.runtime.Sputnik
import
org.spockframework.runtime.Sputnik
...
@@ -18,24 +23,29 @@ import spock.lang.Specification
...
@@ -18,24 +23,29 @@ import spock.lang.Specification
*/
*/
@RunWith
(
PowerMockRunner
.
class
)
@RunWith
(
PowerMockRunner
.
class
)
@PowerMockRunnerDelegate
(
Sputnik
.
class
)
@PowerMockRunnerDelegate
(
Sputnik
.
class
)
@PrepareForTest
()
class
DemoSpockMock
extends
Specification
{
class
DemoSpockMock
extends
Specification
{
def
shoppingCartNewServiceImpl
=
new
ShoppingCartNewServiceImpl
()
def
shoppingCartNewServiceImpl
=
new
ShoppingCartNewServiceImpl
()
def
activityAdapter
=
Mock
(
ActivityAdapter
.
class
)
def
customerInfoManager
=
PowerMockito
.
mock
(
AssortmentCustomerInfoManager
)
void
setup
()
{
void
setup
()
{
shoppingCartNewServiceImpl
.
activityAdapter
=
activityAdapt
er
shoppingCartNewServiceImpl
.
customerInfoManager
=
customerInfoManag
er
}
}
def
"testMockEnable"
()
{
def
"testMockEnable"
()
{
given:
"设置请求参数"
given:
"设置请求参数"
def
customerInfoVo
=
JSON
.
parseObject
(
"{\"accessToken\":\"\",\"alipayUserId\":\"\",\"appId\":\"wx3f7ca0c75b213380\",\"appSecret\":\"\",\"appsecret\":\"\",\"channel\":\"1\",\"customerId\":\"\",\"devBrand\":\"\",\"devModel\":\"\",\"iappId\":\"7\",\"memberId\":\"3614319825989629832\",\"mobile\":\"17327190802\",\"newMember\":false,\"nickName\":\"SWEET SUGA\",\"openId\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"openid\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"partnerId\":\"2581\",\"photoUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/zahIlicTNSM0IA0qjOryv6s0SzIhlwW9EpQKvL63GbPhBwficMAibDohDEibSCQU0I5KohibIpp0yL57TWRcVKrRH2A/132\",\"registerTime\":\"2021-02-26 14:10:26\",\"sessionId\":\"eac53a53f8ae62def7f40cb1e47a1bd9791d95a8\",\"sessionKey\":\"GCyyapzbBSM4yXh9AduGOw==\",\"session_key\":\"GCyyapzbBSM4yXh9AduGOw==\",\"storeId\":\"\",\"tableNumber\":\"\",\"thirdPartyMemberID\":\"\",\"thirdPartyPartnerID\":\"\",\"unionId\":\"o7cEm68JPFhbrzqb0a0BcQE-dDVA\",\"wxAppId\":\"wx3f7ca0c75b213380\",\"wxAppid\":\"wx3f7ca0c75b213380\"}"
def
customerInfoVo
=
JSON
.
parseObject
(
"{\"accessToken\":\"\",\"alipayUserId\":\"\",\"appId\":\"wx3f7ca0c75b213380\",\"appSecret\":\"\",\"appsecret\":\"\",\"channel\":\"1\",\"customerId\":\"\",\"devBrand\":\"\",\"devModel\":\"\",\"iappId\":\"7\",\"memberId\":\"3614319825989629832\",\"mobile\":\"17327190802\",\"newMember\":false,\"nickName\":\"SWEET SUGA\",\"openId\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"openid\":\"o98fP4qjUo7xm-nT-JAaVEk9yBqM\",\"partnerId\":\"2581\",\"photoUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/zahIlicTNSM0IA0qjOryv6s0SzIhlwW9EpQKvL63GbPhBwficMAibDohDEibSCQU0I5KohibIpp0yL57TWRcVKrRH2A/132\",\"registerTime\":\"2021-02-26 14:10:26\",\"sessionId\":\"eac53a53f8ae62def7f40cb1e47a1bd9791d95a8\",\"sessionKey\":\"GCyyapzbBSM4yXh9AduGOw==\",\"session_key\":\"GCyyapzbBSM4yXh9AduGOw==\",\"storeId\":\"\",\"tableNumber\":\"\",\"thirdPartyMemberID\":\"\",\"thirdPartyPartnerID\":\"\",\"unionId\":\"o7cEm68JPFhbrzqb0a0BcQE-dDVA\",\"wxAppId\":\"wx3f7ca0c75b213380\",\"wxAppid\":\"wx3f7ca0c75b213380\"}"
,
AssortmentCustomerInfoVo
.
class
)
,
AssortmentCustomerInfoVo
.
class
)
def
getShoppingCartGoodsApportionRequestVo
=
JSON
.
parseObject
(
"{\"shoppingCartInfoRequestVo\":{\"activityCode\":\"G1643164091158349\",\"buyType\":0,\"couponCode\":\"88709227085491207040\",\"couponCodes\":[{\"activityCode\":\"G1643164091158349\",\"couponCode\":\"88709227085491207040\",\"couponType\":7,\"discountAmount\":800,\"index\":1}],\"flag\":0,\"menuType\":\"saas\",\"orderType\":1,\"partnerId\":\"2581\",\"reachStoreType\":5,\"sendGoods\":[],\"sessionId\":\"eac53a53f8ae62def7f40cb1e47a1bd9791d95a8\",\"shopId\":\"229830\",\"trackingNo\":\"8283bc4c7e5f4bbb8b357e3c0bef7183\",\"useCouponFlag\":1,\"useCustomerScore\":2,\"version\":\"2.0.60.6\"}}"
,
GetShoppingCartGoodsApportionRequestVo
.
class
)
def
premiumExchangeActivity
=
getShoppingCartGoodsApportionRequestVo
.
getPremiumExchangeActivity
()
def
shoppingCartInfoRequestVo
=
getShoppingCartGoodsApportionRequestVo
.
getShoppingCartInfoRequestVo
()
and:
"mock掉接口返回的用户信息"
and:
"mock掉接口返回的用户信息"
// customerInfoManager.getCustomerInfoByObject(_) >> customerInfoVo
Mockito
.
when
(
customerInfoManager
.
getCustomerInfoByObject
(
Mockito
.
anyString
())).
thenReturn
(
customerInfoVo
)
when:
"调用获取用户信息方法"
when:
"调用获取用户信息方法"
def
response
=
shoppingCartNewServiceImpl
.
getShoppingCartGoods
(
_
,
_
)
def
response
=
shoppingCartNewServiceImpl
.
getShoppingCartGoods
(
shoppingCartInfoRequestVo
,
premiumExchangeActivity
)
then:
"验证返回结果是否符合预期值"
then:
"验证返回结果是否符合预期值"
}
}
}
}
shopping-cart-application-service/src/test/spock/cn/freemud/service/impl/FishermanSpock.groovy
View file @
b778b8ff
...
@@ -24,6 +24,7 @@ import com.alibaba.fastjson.JSON
...
@@ -24,6 +24,7 @@ import com.alibaba.fastjson.JSON
import
com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import
com.freemud.api.assortment.datamanager.entity.vo.AssortmentCustomerInfoVo
import
com.freemud.api.assortment.datamanager.manager.customer.AssortmentCustomerInfoManager
import
com.freemud.api.assortment.datamanager.manager.customer.AssortmentCustomerInfoManager
import
org.junit.runner.RunWith
import
org.junit.runner.RunWith
import
org.mockito.Mockito
import
org.powermock.api.mockito.PowerMockito
import
org.powermock.api.mockito.PowerMockito
import
org.powermock.api.support.membermodification.MemberMatcher
import
org.powermock.api.support.membermodification.MemberMatcher
import
org.powermock.core.classloader.annotations.PrepareForTest
import
org.powermock.core.classloader.annotations.PrepareForTest
...
@@ -51,6 +52,18 @@ class FishermanSpock extends Specification {
...
@@ -51,6 +52,18 @@ class FishermanSpock extends Specification {
def
itemService
=
new
ItemServiceImpl
()
def
itemService
=
new
ItemServiceImpl
()
def
storeItemAdapter
=
new
StoreItemAdapter
()
def
storeItemAdapter
=
new
StoreItemAdapter
()
def
defaultPromotionService
=
new
DefaultPromotionService
()
def
defaultPromotionService
=
new
DefaultPromotionService
()
def
couponPromotionService
=
new
CouponPromotionService
()
def
buySendPromotionService
=
new
BuySendPromotionService
()
def
additionalPromotionService
=
new
AdditionalPromotionService
()
def
timeSalePromotionService
=
new
TimeSalePromotionService
()
def
setMealService
=
new
SetMealServiceImpl
()
def
customerScoreService
=
new
CustomerScoreService
()
def
xyDiscountService
=
new
XyDiscountServiceImpl
()
def
buyAndGiftsPromotionService
=
new
BuyAndGiftsPromotionService
()
def
materialPromotionService
=
new
MaterialPromotionService
()
def
fullPromotionService
=
new
FullPromotionService
()
def
assortmentSdkService
=
PowerMockito
.
mock
(
AssortmentSdkService
)
def
assortmentSdkService
=
PowerMockito
.
mock
(
AssortmentSdkService
)
def
customerInfoManager
=
PowerMockito
.
mock
(
AssortmentCustomerInfoManager
.
class
)
def
customerInfoManager
=
PowerMockito
.
mock
(
AssortmentCustomerInfoManager
.
class
)
def
productService
=
PowerMockito
.
mock
(
ProductServiceImpl
.
class
)
def
productService
=
PowerMockito
.
mock
(
ProductServiceImpl
.
class
)
...
@@ -121,19 +134,29 @@ class FishermanSpock extends Specification {
...
@@ -121,19 +134,29 @@ class FishermanSpock extends Specification {
and:
"调三方反馈参数 为自己的参数对象 "
and:
"调三方反馈参数 为自己的参数对象 "
customerInfoManager
.
getCustomerInfoByObject
(
_
)
>>
customerInfoVo
Mockito
.
when
(
assortmentSdkService
.
getShoppingCart
(
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
())).
thenReturn
(
goodList
)
assortmentSdkService
.
getShoppingCart
(
_
,
_
,
_
,
_
,
_
,
_
,
_
)
>>
goodList
Mockito
.
when
(
customerInfoManager
.
getCustomerInfoByObject
(
Mockito
.
any
())).
thenReturn
(
customerInfoVo
)
productService
.
hasRequiredProducts
(
_
,
_
,
_
,
_
)
>>
hasRequiredProducts
Mockito
.
when
(
productService
.
hasRequiredProducts
(
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
())).
thenReturn
(
hasRequiredProducts
)
assortmentSdkService
.
checkShoppingCartSdk
(
_
,
_
,
_
,
_
,
_
,
_
,
_
,
_
)
>>
checkCartRequest
Mockito
.
when
(
assortmentSdkService
.
checkShoppingCartSdk
(
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
(),
Mockito
.
any
())).
thenReturn
(
checkCartRequest
)
activityClient
.
calculationDiscount
(
_
)
>>
activityCalculationDiscountResponseDto
Mockito
.
when
(
activityClient
.
calculationDiscount
(
Mockito
.
any
())).
thenReturn
(
activityCalculationDiscountResponseDto
)
commonService
.
getOrgIdsAsString
(
_
,
_
)
>>
null
Mockito
.
when
(
commonService
.
getOrgIdsAsString
(
Mockito
.
any
(),
Mockito
.
any
())).
thenReturn
(
null
)
commonService
.
getOrgIdsForCoupon
(
_
,
_
)
>>
[
"1e906bab-1800-4b00-8e36-29049d7f4efc"
,
"391cec83-2b60-49a9-af2c-d8d6a813ce72"
,
"16dc665e-8e03-41a8-83ae-95e92666d266"
]
Mockito
.
when
(
commonService
.
getOrgIdsForCoupon
(
Mockito
.
any
(),
Mockito
.
any
())).
thenReturn
([
"1e906bab-1800-4b00-8e36-29049d7f4efc"
,
"391cec83-2b60-49a9-af2c-d8d6a813ce72"
,
"16dc665e-8e03-41a8-83ae-95e92666d266"
])
couponOnlineClient
.
getCouponDetails
(
_
)
>>
responseDto
Mockito
.
when
(
couponOnlineClient
.
getCouponDetails
(
Mockito
.
any
())).
thenReturn
(
responseDto
)
productClient
.
listProductInfos
(
_
)
>>
productInfosDto
Mockito
.
when
(
productClient
.
listProductInfos
(
Mockito
.
any
())).
thenReturn
(
productInfosDto
)
productClient
.
validateShopProduct
(
_
)
>>
productBaseResponse
Mockito
.
when
(
productClient
.
validateShopProduct
(
Mockito
.
any
())).
thenReturn
(
productBaseResponse
)
and:
"静态方法mock"
and:
"静态方法mock"
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
DEFAULT_PROMOTION
)).
thenReturn
(
defaultPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
DEFAULT_PROMOTION
)).
thenReturn
(
defaultPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
COUPON_DISCOUNT
)).
thenReturn
(
couponPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
BUY_ONE_SEND
)).
thenReturn
(
buySendPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
ADDITIONAL_BUY
)).
thenReturn
(
additionalPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
TIME_SALE
)).
thenReturn
(
timeSalePromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
SET_MEAL
)).
thenReturn
(
setMealService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
CUSTOMER_SCORE
)).
thenReturn
(
customerScoreService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
XY_DISCOUNT
)).
thenReturn
(
xyDiscountService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
BUY_GIFTS
)).
thenReturn
(
buyAndGiftsPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
Material
)).
thenReturn
(
materialPromotionService
)
PowerMockito
.
when
(
PromotionFactory
.
getPromotionService
(
ShoppingCartPromotionEnum
.
FULL_REDUCTION
)).
thenReturn
(
fullPromotionService
)
when:
"调用方法逻辑"
when:
"调用方法逻辑"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment