我还是Junit
测试新手。我有一个switch-case代码如下。
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);
}
这是我如何使用@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);
}
到目前为止测试显示通过了。但还有其他方法来测试switch-case
吗?测试开关盒的最佳方法是什么,以便测试所有可能的值?任何帮助将不胜感激。
提前致谢!
答案 0 :(得分:1)
您似乎正在尝试测试manageTrans
,但由于您的代码结构(混合业务和持久性逻辑)而以奇怪的方式进行测试。
您可以使用generateEntry(ISOMsgZxc)
创建并返回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;
}
这将允许您测试generateEntry
以验证输入后的条目:
@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));
}
在您的生产代码中,只需致电:
entryRepo.saveAndFlush(generateEntry())
易于维护(验证的空间),更容易测试(关注点被分开)。
假设您要测试持久性部分,则应创建另一个测试。
manageTrans
看起来像这样:
public void manageTrans(ISOMsgZxc isoMsgZxc) {
AbcEntry entry = generateEntry();
entryRepo.saveAndFlush(entry);
}
您的测试只会在调用manageTrans
后检查回购中是否存在该条目。虽然可能性saveAndFlush
已经过测试,但manageTrans
确实不需要测试,因为它的实现包含已经过测试的代码,并且不需要特殊的集成。
答案 1 :(得分:0)
第三个测试用例的测试可能如下所示。
@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);
}
涵盖其他测试用例:
isoMsgZxc.setMti("0200");
和isoMsgZxc.setProcessingCode("220012");
,以便他们输入正确的案例