dimanche 18 juin 2017

JUnit Test with switch-case

I still new to Junit test. I have a switch-case as code below.

public void manageTrans(ISOMsgZxc isoMsgZxc) {
        AbcEntry abcEntry = new AbcEntry();
        abcEntry.setEntryMid(isoMsgZxc.getMid());

        String mti = isoMsgZxc.getMti() + "." + isoMsgZxc.getProcessingCode().substring(0, 2);
        String transType = "";
        BigDecimal amt = new BigDecimal("00.000");

        switch (mti) {
            case "1234.14":
            case "0212.02":
                transType = "S";
                amt = new BigDecimal(isoMsgZxc.getTransactionAmount()).negate();
                break;
            case "0400.20":
            case "0200.22":
                transType = "R";
                amt = new BigDecimal(isoMsgZxc.getTransactionAmount());
                break;
        }

        abcEntry.setEntryType(transType);
        acbEntryRepository.saveAndFlush(jcbEntry);
    }

Here how I testing it by using @Test

 @Test
    public void manageTrans() throws Exception {

        AbcEntry abcEntry = mock(abcEntry.class);

        PowerMockito.whenNew(AbcEntry.class).withNoArguments()
                .thenReturn(abcEntry);

        ISOMsgZxc isoMsgZxc = new ISOMsgZxc();
        isoMsgZxc.setMti("0100");
        isoMsgZxc.setMid("0100");
        isoMsgZxc.setProcessingCode("000012");
        isoMsgZxc.setTransactionAmount("00.000");

        txnService.manageTrans(isoMsgZxc);

        verify(abcEntry).setEntryMid(isoMsgZxc.getMid());

        String asd = "0400.20";
        if(asd.equals("0400.20") || (mti.equals("0200.02")))
        {
            verify(jcbEntry).setEntryType("R");
        }

        verify(jcbEntryRepositoryMock).saveAndFlush(jcbEntry);
    }

So far the testing show pass. But are there any others method to test the switch-case ? What is the best way to test the switch case so all the possible value can be tested? Any help would be greatly appreciated.

Thanks in advance !

Aucun commentaire:

Enregistrer un commentaire