I am looking at the following code
import mockit.Mock;
import mockit.MockUp;
new MockUp<Fubar>() {
@Mock
delete(final String fooId) {
assertEquals(fooId, "foo123");
}
};
I note that this is not being assigned to any variable. I am guessing that somehow (and I read about class loaders someplace) this causes every instance of Fubar to be defined with this mocked method delete, even if it is instantiated elsewhere. (I would guess this happens in such a way that timing does not matter?) Is there something called Mockit which is different than Mockito? I have seen this pattern more than once in the existing code, so I do not believe it is a typo.
EDIT: Please do not downvote this without explanation as to why. This is a legit question that I have already spent significant time attempting to research.
EDIT: Not one post discussed what the code's probable intention is but instead dwelt on my failure to identify what mocking framework was used.
It redefines the delete method in Fubar
to contain the mocked version instead. This affects all instances created after, until the original is restored at the end of each test. This is possible because it uses its own Java agent which allows redefining classes as the program is running.
For example:
import mockit.Mock;
import mockit.MockUp;
class Fubar {
public void delete(String fooId) {
System.out.println("Called real");
}
}
public class Main {
public static void main(String[] args) throws Exception {
new MockUp<Fubar>() {
@Mock
void delete(final String fooId) {
System.out.println("Called mock");
}
};
new Fubar().delete("foo123");
}
}
Output is: "Called mock"