2013-10-24 40 views
0

“增加了更多的细节”忽略使用了EasyMock使用JUnit

我想嘲弄某个空白方法方法/无效的方法,但我不太清楚如何。我读了EasyMock,但当它是一个无效的方法时我不知道该怎么办,这是我的主要课程;

主类

public class Main { 
    Updater updater = new Updater(main.getID(), main.getName(),....); 

    try { 
     updater.updateContent(dir); 
    } 

我想嘲弄updater.updateContent(dir);,这样我可以跳过尝试

Updater类

private String outD; 

public void updateContent(final String outDir) throws Exception { 


    outD = outDir; 
    if (...) { 
     ....; 
    } 

} 

...私人沃伊d方法

这是我的测试类到目前为止,

public class MainTest { 


    @Before 
    public void setUp() { 

    } 

    @Test 
    public void testMain() { 


try { 


     try { 
      Updater updater = EasyMock.createNiceMock(Updater.class); 
      updater.updateContent("/out"); 
      EasyMock.expectLastCall().andThrow(new RuntimeException()); 

      EasyMock.replay(updater); 

      updater.updateContent("/out"); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 


} 
    } 

(编辑)感谢。

+1

“跳过试试”是什么意思? – dkatzel

+0

@dkatzel意思是我不想执行'updater.updateContent(dir);' – Hash

回答

1

对于返回void必须记录的行为,这样的方法:

Updater updater = EasyMock.createNiceMock(Updater.class); 
updater.updateContent("someDir"); // invoke the easy mock proxy and 
// after invoking it record the behaviour. 
EasyMock.expectLastCall().andThrow(new RuntimeException()); // for example 

EasyMock.replay(updater); 

updater.updateContent("someDir"); // will throw the RuntimeException as recorded 

期待您有以下Main

public class Main { 
    private Updater updater; 

    private int updatedContentCount; // introduced for the example 

    public Main(Updater updater) { 
     this.updater = updater; 
    } 

    public void updateContent() { 
     try { 
      updater.updateContent("/out"); 
      updatedContentCount++; 
     } catch (Exception e) { 
      // skip for this example - normally you should handle this 
     } 
    } 

    public int getUpdatedContentCount() { 
     return updatedContentCount; 
    } 

} 

和你更新的API看起来像这样

public class Updater { 

    public void updateContent(String dir) throws Exception { 
     // do something 
    } 
} 

然后Main类的测试将是somet兴这样的:

public class MainTest { 

    private Updater updater; 
    private Main main; 

    @Before 
    public void setUp() { 
     updater = EasyMock.createNiceMock(Updater.class); 
     main = new Main(updater); 
    } 

    @Test 
    public void testUpdateCountOnException() throws Exception { 
     updater.updateContent("/out"); 
     EasyMock.expectLastCall().andThrow(new RuntimeException()); 
     EasyMock.replay(updater); 
     main.updateContent(); 
     int updatedContentCount = main.getUpdatedContentCount(); 
     Assert.assertEquals(
       "Updated count must not have been increased on exception", 0, 
       updatedContentCount); 
    } 
} 

MainTest测试,如果使用UpdateCount正确处理在Updater的异常。

+0

我添加了你给出的请看一看,看看有什么问题 – Hash

+0

目前我无法弄清楚你的类是如何工作的。不过,我会更新我的答案,并向您展示如何进行测试。 –

+0

当我用junit运行它时,它仍然进入'updateContent'方法 – Hash