Once in a while, when testing your Objective-C code, you want or have to mock a class method. Or you want to replace the implementation of a class method. The following code replaces the implementation of originalClassMethod of SomeClass with the implementation of mockClassMethod of MockClass:
Method mockMethod = class_getInstanceMethod([MockClass class], @selector(mockClassMethod)); IMP mockImpl = method_getImplementation(mockMethod); Method originalMethod = class_getClassMethod([SomeClass class], @selector(originalClassMethod)); IMP originalImpl = method_setImplementation(originalMethod, testImpl);
Don’t forget to import objc/runtime.h.