Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://code.google.com/p/powermock/wiki/MockConstructor" rel="nofollow">This guide</a> details mocking a constructor call using PowerMock:</p> <blockquote> <p>... But with PowerMock you don't have to change the code, instead you can instruct PowerMock to intercept the call to new File(..) and return a mock object instead. To do this we start by creating a mock of type File.class as usual: </p> <pre><code>File fileMock = createMock(File.class); </code></pre> <p>To expect the call to new File we simply do:</p> <pre><code>expectNew(File.class, "directoryPath").andReturn(fileMock); </code></pre> </blockquote> <p>So in your case, you should have these in your <strong>test case or setup method</strong>:</p> <pre><code>MyObject mockOb = createMock(MyObject.class); expectNew(MyObject.class).andReturn(mockOb); </code></pre> <p>By adding these, you will have the following behaviour:</p> <p>Whenever there is a <code>new MyObject()</code> in <strong>your code</strong> run by the test case, the object instance mocked by PowerMock will be returned. So it does not matter <em>where</em> in your code you create a new <code>MyObject</code> instance, it will be the very same one created by the <code>createMock()</code> function.</p> <p><strong>EDIT</strong></p> <blockquote> <p>lets take example </p> <pre><code>Class A { private void method1() { Obj obj = new Obj(); obj.setX(10); } //Here I want to access the obj object to verify the values } </code></pre> </blockquote> <p>To accomplish this, you have to think a bit differently.</p> <ol> <li>create real A instance</li> <li>set up mocking for the Obj constructor</li> <li>run your logic that triggers the <code>pruivate method1()</code></li> <li>check the values <em>on the mocked Obj instance</em> <ul> <li>if the values are not testable (something modifies it in the logic), zou could check if the setX() was called the appropriate times.</li> </ul></li> </ol> <p>Something along these lines:</p> <pre><code>@Test public void testMyPrivateMethod() { //1. object with the logic to test A a = new A(); //2. set up mocking Obj mockObj = createMock(Obj.class); expectNew(Obj.class).andReturn(mockObj); //3. trigger logic to test a.someOtherMethodThatCallsMethod1(); //4. test Obj (find out if setX() has been called or not) verify(mockObj).setX(any(Integer.class)); } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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