2010-01-27 70 views
5

使用最新版本的EasyMock,我有一個方法,我需要將其取出。該方法接受一個對象參數並返回void。EasyMock:在編譯時提供你不知道的參數

存根方法正在被我測試的方法調用。那裏沒有驚喜。我的困難是,作爲模擬方法的參數提供的對象是由我測試的方法創建的。

我知道我可以使用createNiceMock()解決這個問題,但是有沒有一種方法可以明確地將這種方法存根?

示例代碼:

public interface IMockMe { 
    void doSomething(InnerObj obj); 
} 

public class TestMe { 
    IMockMe mockMe; 

    public void testThisMethod() { 
     InnerObj obj = new InnerObj(); 
     mockMe.doSomething(obj); 
    } 
} 

class Tester { 
    @Test 
    public void testThatDarnedMethod() { 
     IMockMe mocked = EasyMock.create(IMockMe.class); 

     mocked.doSomething(/* what goes here? */); 
     EasyMock.expectLastCall(); 

     TestMe testMe = new TestMe(mocked); 
     testMe.testThisMethod(); 

    } 
} 

回答

4

看一看了「靈活的期望與參數匹配器」的EasyMock documentation的部分。從文檔樣本:

String[] documents = new String[] { "Document 1", "Document 2" }; 
expect(mock.voteForRemovals(aryEq(documents))).andReturn(42); 

aryEq(documents)是它創建了一個匹配器,將任何陣列匹配的權限內容,而不是由身份匹配的呼叫。

就你而言,你可能需要​​匹配器。

+0

美麗,謝謝! – roufamatic 2010-01-27 17:43:46

+0

斷開的鏈接。請參閱:http://easymock.org/user-guide.html#verification-expectations – aglassman 2014-09-16 21:14:04

+0

@aglassman:修正,謝謝。 – 2014-09-17 05:47:26

相關問題