mockito springtest junit

利用mockito 进行单元测试  案例

pom文件配置



mockito springtest junit_第1张图片




package test;

代码



import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.InjectMocks;

import org.mockito.Matchers;

import org.mockito.Mock;

import org.mockito.Mockito;

import org.mockito.MockitoAnnotations;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.unicompayment.fup.api.FUPSysFacade;

import com.unicompayment.fup.api.dto.FupFundBuyReq;

import com.unicompayment.fup.api.dto.FupFundBuyResp;

import com.unicompayment.fup.api.dto.FupResHeadInfo;

import com.unicompayment.fup.common.constants.FUPConstants;

import com.unicompayment.xcsp.api.dto.XCSPPayReq;

import com.unicompayment.xcsp.api.dto.XCSPPayResp;

import com.unicompayment.xcsp.bps.facade.impl.XCSPSysFacadeImpl;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring-context.xml")

//加载配置

@ActiveProfiles("development")

public class XSPSysFacadeImplTest {

@InjectMocks

XCSPSysFacadeImpl xcsSysFacade = new XCSPSysFacadeImpl();

@Mock

FUPSysFacade fupSysFacade;

@Before

public void setUp() {

MockitoAnnotations.initMocks(this);

}

@Test

public void payTestSuccess() {

FupFundBuyReq ffbReq = new FupFundBuyReq();

FupFundBuyResp ffbResp = new FupFundBuyResp();

ffbResp.setResCode(FupResHeadInfo.RESP_SUCCESS_CODE);

ffbResp.setResDesc("success");

ffbResp.setTradeCfmDate("123123");

ffbResp.setShowIncomeDate("1111111");

Mockito.when(this.fupSysFacade.fundBuy((FupFundBuyReq) Matchers.anyObject())).thenReturn(ffbResp);

XCSPPayReq req = new XCSPPayReq();

req.setUserNo("123123");

req.setFundCode("12312");

req.setAssetType("C");

req.setProtocolNoEnc("123121321");

req.setAmount(100L);

XCSPPayResp resp = this.xcsSysFacade.pay(req);

Assert.assertNotNull(resp);

Assert.assertEquals("预期返回0000", FUPConstants.TRADE_RETURN_CODE_NUOAN_S,

resp.getResCode());

Assert.assertEquals("预期返回123123", "123123",

resp.getTradeCfmDate());

Assert.assertEquals("预期返回1111111", "1111111",

resp.getShowIncomeDate());

}

@Test

public void payTestFail() {

FupFundBuyReq ffbReq = new FupFundBuyReq();

FupFundBuyResp ffbResp = new FupFundBuyResp();

ffbResp.setResCode(FupResHeadInfo.RESP_SYSERROR_CODE);

ffbResp.setResDesc("fail");

Mockito.when(this.fupSysFacade.fundBuy((FupFundBuyReq) Matchers.anyObject())).thenReturn(ffbResp);

XCSPPayReq req = new XCSPPayReq();

req.setUserNo("123123");

req.setFundCode("12312");

req.setAssetType("C");

req.setProtocolNoEnc("123121321");

req.setAmount(100L);

XCSPPayResp resp = this.xcsSysFacade.pay(req);

Assert.assertNotNull(resp);

Assert.assertEquals("预期返回9999", FupResHeadInfo.RESP_SYSERROR_CODE,

resp.getResCode());

}

}

你可能感兴趣的:(mockito springtest junit)