简体   繁体   English

EasyMock通过可运行性测试空隙

[英]EasyMock Testing Void With Runnable

I'm trying to test the following class (I've left out the implementation) 我正在尝试测试以下类(我省略了实现)

public class UTRI implements UTR {
    public void runAsUser(String userId, Runnable r);
}

This is the way I would use it: 这就是我使用它的方式:

UTRI.runAsUser("User1", new Runnable () {
    private void run() {
    //do whatever needs to be done here.

    }
});

The problem is, I don't know how to use EasyMock to test functions that return void. 问题是,我不知道如何使用EasyMock来测试返回void的函数。 That and I'm also not too familiar with testing in general (right out of school!). 而且,我对测试也不太熟悉(就在学校外面!)。 Can someone help explain to me what I need to do to approach this? 有人可以帮我解释一下我需要做些什么吗? I was thinking about making the UTRI a mock and doing expectlastcall after that, but realistically, not sure. 我当时正在考虑使UTRI成为模拟对象,然后再进行Expectlastcall,但实际上并不确定。

public class UTRITest {

    UTRI utri = new UTRI();

    @Test
    public void testRunAsUser() {
        // Create Mocks
        Runnable mockRunnable = EasyMock.createMock(Runnable.class);

        // Set Expectations
        **mockRunnable.run();
        EasyMock.expectLastCall().once();**

        EasyMock.replay(mockRunnable);
        // Call the method under test
        utri.runAsUser("RAMBO", **mockRunnable**);

        // Verify if run was called on Runnable!!
        EasyMock.verify(mockRunnable);
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM