简体   繁体   中英

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);
        abcEntryRepository.saveAndFlush(abcEntry);
    }

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(abcEntry).setEntryType("R");
        }

        verify(abcEntryRepositoryMock).saveAndFlush(abcEntry);
    }

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 !

It seems like you're trying to test manageTrans , but in a strange fashion due to the structure of your code (mixing business and persistence logic).

You could have a generateEntry(ISOMsgZxc) which creates and returns the AbcEntry :

public AbcEntry generateEntry(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);
        return abcEntry;
}

This will allow you to test generateEntry to verify the entry after:

@Test
public void generateEntry() {
    ISOMsgZxc isoMsgZxc = new ISOMsgZxc();
    isoMsgZxc.setMti("0100");
    isoMsgZxc.setMid("0100");
    isoMsgZxc.setProcessingCode("000012");
    isoMsgZxc.setTransactionAmount("00.000");

    AbcEntry entry = txnService.generateEntry(isoMsgZxc);

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

    Map<String, String> expectedValues = new HashMap<>();
    expectedValues.put("0400.20", "R");
    expectedValues.put("0200.02", "R");
    //...

    expectedValues.forEach((input, output) -> verify(input).setEntryType(output));
}

In your production code, simply call:

entryRepo.saveAndFlush(generateEntry())

Easier to maintain (room for validation), easier to test (concerns are separated).


Assuming you want to test the persistence part, you should create another test.

manageTrans would look like this:

public void manageTrans(ISOMsgZxc isoMsgZxc) {
    AbcEntry entry = generateEntry();
    entryRepo.saveAndFlush(entry);
}

And your test would simply check if the entry exists in the repo after calling manageTrans . Although chances are, saveAndFlush has already been tested, so the manageTrans really wouldn't need testing, as it's implementation consists of already-tested code, and there is no special integration required.

The test for the third test case could look like this.

@Test
public void manageTrans() throws Exception {
    ISOMsgZxc isoMsgZxc = new ISOMsgZxc();
    isoMsgZxc.setMti("0200");
    isoMsgZxc.setMid("0100");
    isoMsgZxc.setProcessingCode("220012"); 
    isoMsgZxc.setTransactionAmount("00.000");
    //mti should now be = 0200.00, the third case

    txnService.manageTrans(isoMsgZxc);

    assertThat(abcEntry.getEntryMid(), equalTo(isoMsgZxc.getMid()));
    assertThat(abcEntry.getEntryType(), equalTo("R"));

    verify(jcbEntryRepositoryMock).saveAndFlush(jcbEntry);
}

To cover the other test cases:

  • modify the two lines isoMsgZxc.setMti("0200"); and isoMsgZxc.setProcessingCode("220012"); such that they enter the correct case
  • you will need 5 tests for full coverage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM