My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
MockPartial  

Usage
Updated Feb 4, 2010 by johan.ha...@gmail.com

Partial Mocking

Quick summary

  1. Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
  2. Use the @PrepareForTest(ClassToPartiallyMock.class) annotation at the class-level of the test case.
  3. se PowerMock.createPartialMock(ClassToPartiallyMock.class, "nameOfTheFirstMethodToMock", "nameOfTheSecondMethodToMock") to create a mock object that only mocks the methods with name nameOfTheFirstMethodToMock and nameOfTheSecondMethodToMock in this class (let's call it mockObject).
  4. Use PowerMock.replay(mockObject) to change the mock object to replay mode.
  5. Use PowerMock.verify(mockObject) to change the mock object to verify mode.

Example

TODO

References

TODO

Comment by jimi_i_s...@hotmail.com, Feb 4, 2010

For those that are having problems with non-initialized fields: the PowerMock?.createPartialMock method doesn't invoke the constructor and doesn't initialize the fields. This can cause strange behavior when testing, and NullPointerExceptions?.

The solution is to use the method PowerMock?.createPartialMockAndInvokeDefaultConstructor instead.

This was something I had problems with untill Johan Haleby helped me out in this forum discussion: http://groups.google.com/group/powermock/browse_thread/thread/b6634ccfcb581f2f

Comment by kau...@gmail.com, Dec 2, 2010

What if we want to invoke a constructor taking some parameter and also partially mock some methods.

Comment by project member johan.ha...@gmail.com, Dec 6, 2010

You can use createPartialMock(Class<T> type, String methodNames, Object... constructorArguments) and createPartialMockAndInvokeDefaultConstructor. Please use the mailing-list for asking questions in the future.

Comment by VictorDFB, May 17, 2011

What is the simplest way to partially mock a super-class method?

Comment by project member johan.ha...@gmail.com, May 17, 2011

To my knowledge this is not possible in Java since that will break polymorphism.

Comment by moti...@gmail.com, Feb 9, 2012

Use mockStaticPartial method to mock partially a class with static methods. Example:

package mgmt.api;

class CustomerService? {

public static void add() {
subscribeToNewsletter(); another(); System.out.println("add");
}
static void subscribeToNewsletter() {
System.out.println("subscribeTOnewsLetter");
}

static String another() {
System.out.println("another"); return "Another";
}
}

package mgmt.api;

import static org.powermock.api.easymock.PowerMock?.expectLastCall; import static org.powermock.api.easymock.PowerMock?.mockStaticPartial; import static org.powermock.api.easymock.PowerMock?.replayAll;

import org.junit.Test; import org.junit.runner.RunWith?; import org.powermock.core.classloader.annotations.PrepareForTest?; import org.powermock.modules.junit4.PowerMockRunner?;

@RunWith?(PowerMockRunner?.class) @PrepareForTest?(CustomerService?.class) public class CustomerServiceTest? {

@Test public void test() {
mockStaticPartial(CustomerService?.class,"subscribeToNewsletter"); CustomerService?.subscribeToNewsletter(); expectLastCall().once(); replayAll(); CustomerService?.add();
}
}


Sign in to add a comment
Powered by Google Project Hosting