0

I've got two classes as input and want to mock one with the other. That used to be very simple in JMockit, one just called

Mockit.redefineMethods(originalClass, mockingClass);

But in version 0.999 this deprecated method was removed. I need features of a newer version of JMockit, so I cannot use the older versions any more.

I guess from the documentation in the deprecation message that using the proposed "modern" way to do it would be to define a MockUp<originalClass> and use this as the mockingClass.

Unfortunately, I get both values as input parameters at runtime (declared as class<?>), so creating a class is not an option.

Is there any way to emulate what Mockit.redefineMethods() has done before version 0.999, even if it might be not the most elegant solution to address this issue?

EDIT

What I get as input is a Map<Class<?>, Class<?>> mockedClasses of classes to be mocked pointing to classes mocking them. These are then iterated over and passed to Mockit:

for (Map.Entry<Class<?>, Class<?>> entry : mockedClasses.entrySet()) {
  Mockit.redefineMethods(entry.getKey(), entry.getValue());
}

After that, the test code is executed, then the mocking is disabled again, using restoreOriginalDefinition() instead of redefineMethods() in a similar way.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 1
    Not sure this is what you need, but there is a `MockUp(Class)` constructor which receives the class to be mocked. Otherwise, I would need to see an example test, because `MockUp` provides everything that `Mockit.redefineMethods` did in the past. – Rogério Jan 27 '15 at 19:46
  • Thanks, I will try that tomorrow. And if I don't succeed I will try to cook down a minimal example and post that here. – Alfe Jan 27 '15 at 23:17
  • On second thought, no, that's not working for me, sorry. I already *have* both classes, the one to be mocked and the mocking one. Both are arbitrary and my input (and I cannot easily change that). None of them (currently) is extending `MockUp`, and I cannot extend the class I receive as a value with a `MockUp` (at least I don't know how). But maybe there are solutions going that way? – Alfe Jan 28 '15 at 08:47
  • @Rogério, I added a small part showing a little more explicit code on what I am doing. – Alfe Jan 28 '15 at 10:45

1 Answers1

1

Ok, the question is clearer now. And the answer is that there is no way to mock a class with another arbitrary class; you have to define the mock-up class as a subclass of MockUp. The very old Mockit.redefineMethods(Class, Class) (removed from the API 4.5+ years ago) only accepted arbitrary classes because that initial API also supported Java 1.4 for test code (which is no longer supported since 0.999, which required generics and/or annotations).

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • This makes it a problem for me as my legacy code is depending on this removed feature in a very fundamental way. I fear we are going to lose test cases now or have to update each of them manually. But thanks anyway! – Alfe Jan 29 '15 at 14:39