Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had recently implemented the mocking of internal methods with EasyMock for a project we were working on. This was implemented following the EasyMock documentation. See section <em>Partial Mocking</em> in the <a href="http://www.easymock.org/EasyMock3_1_Documentation.html" rel="nofollow">EasyMock documentation</a>.</p> <p>Here is how I wrote the unit test:</p> <p>Lets assume that an API <code>getTopNVideosByTagNameAndProperty</code> in class <code>VideoAPI</code> need to be unit tested and this method internally calls another public method in the same class <code>getVideosByTagName</code>. The internal method takes in a parameter and returns a List. The method <code>getVideosByTagName</code> has to be mocked with EasyMock's mock builder.</p> <p><strong>CODE</strong>:</p> <pre><code>//create the mock object with the help of Mock Builder and then add the method to be mocked. VideoAPI videoApi=EasyMock.createMockBuilder(Video.class).addMockedMethod("getVideosByTagName").createMock(); String tagName ="sometag"; // create the paramter // create a dummy return object you expect from the internal method getVideosByTagName List listVideos = new ArrayList(); for (int i = 1; i &lt;=10; i++) { //populate listVideos } //set up the EasyMock for the mocked method call EasyMock.expect(videoApi.getVideosByTagName(tagName)).andReturn(listVideos).anyTimes(); EasyMock.replay(videoApi); //now call the actual method to be tested - in this case it returns top 5 videos List&lt;Video&gt; listVideoTopN = videoApi.getTopNVideosByTagNameAndProperty("tag", "likes", 5); //do the asserts assertNotNull(listVideoTopN); assertEquals(5, listVideoTopN.size()); //finally verify the mocks EasyMock.verify(videoApi); </code></pre> <p>This is working perfectly fine for me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload