Java Junit验证命令执行
我有一个测试用例:Java Junit验证命令执行,java,junit,Java,Junit,我有一个测试用例: @Test(expected = IllegalArgumentException.class) public void testRunWithLessThanTwoParameters_IllegalArgumentException() throws Exception { final String[] args = { "Less Number of parameters" }; command = spy(new GenerateSumma
@Test(expected = IllegalArgumentException.class)
public void testRunWithLessThanTwoParameters_IllegalArgumentException()
throws Exception {
final String[] args = { "Less Number of parameters" };
command = spy(new GenerateSummaryReportCommand());
commandLine = new PosixParser().parse(new Options(),Arrays.copyOfRange(args, 0, args.length));
assertEquals(0, command.run(commandLine));
verify(command, times(1)).usage();
}
我想知道assertEqual语句之后是否会执行verify我假设
parse
正在抛出IllegalArgumentException
。如果是这种情况,则不会执行assertEquals
或verify
,因为异常是在它们上面抛出的。抛出异常后进行验证的方法是将验证放在catch
块中
@Test(expected = IllegalArgumentException.class)
public void testRunWithLessThanTwoParameters_IllegalArgumentException()
throws Exception {
final String[] args = { "Less Number of parameters" };
command = spy(new GenerateSummaryReportCommand());
try{
commandLine = new PosixParser().parse(new Options(),Arrays.copyOfRange(args, 0, args.length));
}catch (Exception e){
assertEquals(0, command.run(commandLine));
verify(command, times(1)).usage();
throw e;
}
}
请注意,我重新显示了异常,以允许进行
预期的检查。快速回答,如果assertEquals失败,则否。