我有以下单元测试,涉及捕获与传入的无效bean相关的异常:
/**
* Test scenario where someone could enter value
* in commandBeanName that does not exist.
* This will throw MwsException.
*/
@Test(expected=MwsException.class)
public void testCommandBeanNotFound() {
WritebackCommandMetadata writebackCommand = new WritebackCommandMetadata();
writebackCommand.setCommandBeanName("InvalidBean");
writebackCommand.setCommandName("Testing Invalid Bean");
List<WritebackCommandMetadata> commands = new ArrayList<WritebackCommandMetadata>();
commands.add(writebackCommand);
when(configService.getCommands(any())).thenReturn(commands);
WritebackConfigKey configKey = new WritebackConfigKey(EvaluationTypeEnum.FINAL, WritebackType.NOWORKOUT);
beanResolver.getCommands(configKey);
}
当我运行它时,我可以清楚地浏览代码,看到它正在捕获NoSuchBeanDefinitionException
并抛出我的自定义异常。