最近写的需求改动比较多,数据比较单一,最重要是不想加载Spring环境配置,spock框架可以利用stub和mock来模拟spring环境,加载respository之类的跟数据库相关的服务。。这里就mark一下其中一个实例。。。
这里mark一下按天买车险试算服务的相关测试代码。。。该服务的逻辑不赘述。。。
package com.cheche365.cheche.test.core
import com.cheche365.cheche.core.model.User
import com.cheche365.cheche.core.repository.InsuranceCompanyAreaRepository
import com.cheche365.cheche.core.repository.InsuranceRepository
import com.cheche365.cheche.core.service.daily.insurance.TrialDataService
import com.cheche365.cheche.core.util.CacheUtil
import com.cheche365.cheche.test.core.common.EntityCenter
import com.google.gson.JsonParser
import spock.lang.Specification
import spock.lang.Unroll
/**
* Created by chenqc on 2017/2/10.
*/
class DailyInsuranceServiceFT extends Specification {
@Unroll
def "test for trialDataService"() {
given:
JsonParser parser = new JsonParser()
def insuranceRepository = Stub(InsuranceRepository) {
findByUserAndPolicyNoNotNull(_) >> insurance
}
def insuranceCompanyAreaRepository = Stub(InsuranceCompanyAreaRepository) {
findByInsuranceCompanyOrderByWeightDesc(_) >> EntityCenter.insuranceCompanyAreas
}
TrialDataService trialDataService = new TrialDataService(insuranceRepository, insuranceCompanyAreaRepository)
expect:
parser.parse(CacheUtil.doJacksonSerialize(trialDataService.getTrialData(user))) == parser.parse(output.toString())
where:
user | insurance | output
new User(id: 1722l) | EntityCenter.insurance | EntityCenter.output.getJSONObject(0)
null | null | EntityCenter.output.getJSONObject(1)
new User(id: 1722l) | null | EntityCenter.output.getJSONObject(1)
}
}
测试数据有下面的代码加载到程序中
package com.cheche365.cheche.test.core.common
import com.cheche365.cheche.core.model.Agent
import com.cheche365.cheche.core.model.AgentRebate
import com.cheche365.cheche.core.model.CompulsoryInsurance
import com.cheche365.cheche.core.model.Insurance
import com.cheche365.cheche.core.model.InsuranceCompanyArea
import com.cheche365.cheche.core.model.PartnerOrder
import com.cheche365.cheche.core.model.Payment
import com.cheche365.cheche.core.model.QuoteRecord
import com.cheche365.cheche.core.model.User
import com.cheche365.cheche.core.util.CacheUtil
import net.sf.json.JSONArray
/**
* Created by zhengwei on 1/9/17.
*/
class EntityCenter {
static User customer
static User userAgent
static QuoteRecord quoteRecord
static Agent agent
static AgentRebate agentRebate
static List<PartnerOrder> partnerOrderList
static Insurance insurance
static CompulsoryInsurance compulsoryInsurance
static Payment payment
static Payment addtionalPayment
static List<Payment> paymentOneAmendList
static List<Payment> paymentContainChechepayList
static List<Payment> paymentPartialRefundList
static List<Payment> paymentFullRefundList
static List<List<Payment>> paymentList
static List<Payment> allPayments
static Iterable<InsuranceCompanyArea> insuranceCompanyAreas
static JSONArray expectedResult
static JSONArray output
def static CLASS_TO_HANDLER = [
(User.class) : { data ->
customer = data.find { 1 == it.userType.id };
userAgent = data.find { 2 == it.userType.id }
},
(QuoteRecord.class) : { quoteRecord = it[0] },
(Agent.class) : { agent = it[0] },
(AgentRebate.class) : { agentRebate = it[0] },
(PartnerOrder.class) : { partnerOrderList = it },
(Insurance.class) : { insurance = it[0] },
(CompulsoryInsurance.class) : { compulsoryInsurance = it[0] },
(Payment.class) : { allPayments = it },
(InsuranceCompanyArea.class): { insuranceCompanyAreas = it }
]
def static OUTPUT_TO_HANDLER = [
'expectedResult': { expectedResult = it },
'output' : { output = it }
]
static {
CLASS_TO_HANDLER.each { clazz, handler ->
def dataInJson = EntityCenter.class.getResource("common_data_${clazz.simpleName.toLowerCase()}.json").text
handler CacheUtil.doListJacksonDeserialize(dataInJson, clazz)
}
OUTPUT_TO_HANDLER.each { k, handler ->
handler JSONArray.fromObject(EntityCenter.class.getResource("common_data_${k}.json").text)
}
}
static {
paymentOneAmendList = allPayments[0..1]
paymentPartialRefundList = allPayments[0..2]
paymentList = [paymentOneAmendList, paymentPartialRefundList]
}
}
*注意测试数据获取代码和测试数据在同一个包下
包级结构如下