mock代码片断1

6 篇文章 0 订阅
4 篇文章 0 订阅
/*for validator.java*/
public class DemoLegValida{

    public void validateCounterparty(Demosnapshotsnapshot) {
        for (demosnapshot demosnapshot : snapshot.getDemoLegs()) {
            ValidationExceptionUtil.notEmpty(demosnapshot.getCounterParty().getCounterPartyPOGroup(), "POGroup must be specified");
            ValidationExceptionUtil.notEmpty(demosnapshot.getCounterParty().getCounterPartyPOUnit(), "POUnit must be specified");
            ValidationExceptionUtil.notEmpty(demosnapshot.getCounterParty().getCounterPartyIdentifier(), "Counterparty must be specified");
        }
    }

/* UnitTest.java */
public class DemoLegValidatorTest {

    @Test
    public void testcounterpartyValidatorWithNoError() {
        DemoSnapshot snapshot = mock(DemoSnapshot.class);
        List<DemoLegSnapshot> legList = MockDemoLegUtil.getMockedDemoLegSnapshot();
        given(snapshot.getDemoLegs()).willReturn(legList);
        DemoLegValidatorDemoLegValidator= new DemoLegValidator();
        DemoLegValidator.validateCounterparty(snapshot);
    }


    @Test
    public void testcounterpartyValidatorWithEmptyPartyIdentifier() {
        DemoSnapshot snapshot = mock(DemoSnapshot.class);
    List<DemoLegSnapshot> legList = MockDemoLegUtil.getMockedDemoLegSnapshot_emptyCounterPartyIdentifier();
        given(snapshot.getDemoLegs()).willReturn(legList);
        DemoLegValidatorDemoLegValidator= new DemoLegValidator();
        catchException(DemoLegValidator).validateCounterparty(snapshot);
        assertThat(caughtException(),
                allOf(instanceOf(ValidationException.class),
                        hasMessage("Counterparty must be specified")));
    }

    @Test
    public void testcounterpartyValidatorWithEmptyPOGroup() {
        DemoSnapshot snapshot = mock(DemoSnapshot.class);
        List<DemoLegSnapshot> legList = MockDemoLegUtil.getMockedDemoLegSnapshot_emptyCounterPartyPOGroup();
        given(snapshot.getDemoLegs()).willReturn(legList);
        DemoLegValidatorDemoLegValidator= new DemoLegValidator();
        catchException(DemoLegValidator).validateCounterparty(snapshot);
        assertThat(caughtException(),
                allOf(instanceOf(ValidationException.class),
                        hasMessage("POGroup must be specified")));
    }

    @Test
    public void testcounterpartyValidatorWithEmptyPOUnit() {
        DemoSnapshot snapshot = mock(DemoSnapshot.class);
        List<DemoLegSnapshot> legList = MockDemoLegUtil.getMockedDemoLegSnapshot_emptyCounterPartyPOUnit();
        given(snapshot.getDemoLegs()).willReturn(legList);
        DemoLegValidatorDemoLegValidator= new DemoLegValidator();
        catchException(DemoLegValidator).validateCounterparty(snapshot);
        assertThat(caughtException(),
                allOf(instanceOf(ValidationException.class),
                        hasMessage("POUnit must be specified")));
    }

/* mockdata.java */
public class MockDemoLegUtil {
    public static List<DemoLegSnapshot> getMockedDemoLegSnapshot() {
        DemoLegSnapshot mock = mock(DemoLegSnapshot.class);

        given(mock.getBaseCurrency()).willReturn("GBP");
        given(mock.getPct()).willReturn(new BigDecimal(50.0));
        given(mock.getLegId()).willReturn("66666");

        CounterPartyInfo counterPartyInfo_mock = mock(CounterPartyInfo.class);

        given(mock.getCounterParty()).willReturn(counterPartyInfo_mock);
        given(counterPartyInfo_mock.getCounterPartyIdentifier()).willReturn("9999");
        given(counterPartyInfo_mock.getCounterPartyPOGroup()).willReturn("LOH");
        given(counterPartyInfo_mock.getCounterPartyPOUnit()).willReturn("PO7");


        given(mock.getCounterParty()).willReturn(counterPartyInfo_mock);
        given(mock.getBuySell()).willReturn("S");
        given(mock.getTradingAmount()).willReturn(new BigDecimal(5000.0));
        given(mock.getCounterCurrency()).willReturn("USD");
        given(mock.getCounterAmount()).willReturn(new BigDecimal(6170.0));

        List<DemoLegSnapshot> result_list = new ArrayList<>();
        result_list.add(mock);
        return result_list;
    }

    public static List<DemoLegSnapshot> getMockedDemoLegSnapshot_emptyCounterPartyIdentifier() {
        DemoLegSnapshot mock = mock(DemoLegSnapshot.class);

        CounterPartyInfo counterPartyInfo_mock = mock(CounterPartyInfo.class);
        given(mock.getCounterParty()).willReturn(counterPartyInfo_mock);
        given(counterPartyInfo_mock.getCounterPartyIdentifier()).willReturn("");
        given(counterPartyInfo_mock.getCounterPartyPOGroup()).willReturn("LOH");
        given(counterPartyInfo_mock.getCounterPartyPOUnit()).willReturn("PO7");
        List<DemoLegSnapshot> result_list = new ArrayList<>();
        result_list.add(mock);
        return result_list;
    }

    public static List<DemoLegSnapshot> getMockedDemoLegSnapshot_emptyCounterPartyPOGroup() {
        DemoLegSnapshot mock = mock(DemoLegSnapshot.class);

        CounterPartyInfo counterPartyInfo_mock = mock(CounterPartyInfo.class);
        given(mock.getCounterParty()).willReturn(counterPartyInfo_mock);
        given(counterPartyInfo_mock.getCounterPartyPOGroup()).willReturn("");
        given(counterPartyInfo_mock.getCounterPartyIdentifier()).willReturn("xxxx");
        given(counterPartyInfo_mock.getCounterPartyPOUnit()).willReturn("PO7");
        List<DemoLegSnapshot> result_list = new ArrayList<>();
        result_list.add(mock);
        return result_list;
    }


    public static List<DemoLegSnapshot> getMockedDemoLegSnapshot_emptyCounterPartyPOUnit() {
        DemoLegSnapshot mock = mock(DemoLegSnapshot.class);

        CounterPartyInfo counterPartyInfo_mock = mock(CounterPartyInfo.class);
        given(mock.getCounterParty()).willReturn(counterPartyInfo_mock);
        given(counterPartyInfo_mock.getCounterPartyPOUnit()).willReturn("");
        given(counterPartyInfo_mock.getCounterPartyIdentifier()).willReturn("yyyy");
        given(counterPartyInfo_mock.getCounterPartyPOGroup()).willReturn("LOH");
        List<DemoLegSnapshot> result_list = new ArrayList<>();
        result_list.add(mock);
        return result_list;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值