2014-03-26 39 views
0

我已经测试用例:Junit的验证命令执行

@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便后声明将执行

+0

快速回答,如果assertEquals失败,否。 –

回答

0

我认为parse是扔IllegalArgumentException。如果是这种情况,assertEqualsverify都不会被执行,因为异常被抛到了上面。抛出异常后进行验证的方法是将验证放入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; 
    } 
} 

请注意,我重新抛出异常,以允许expected检查。