-3

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.

Jeff
  • 1,513
  • 4
  • 18
  • 34
  • 1
    http://idownvotedbecau.se/noresearch/ if you really spent a **significant** time researching this, then you really need to work on your research skills. – Kayaman Oct 31 '17 at 20:08
  • If you have an obvious link I would appreciate it -- I can find no clear explanation for why you would "new" this up without assigning it. If it affects the class loader so that all instances are now defined in this way, with one over-ridden method, I would like to know. – Jeff Oct 31 '17 at 20:11
  • "I have already spent significant time attempting to research" -> you should have spent a little bit more time formatting your code properly (fixed that already) and making the question clearer. What are you asking about? Are you asking about Mockito? JMockit? Something different? And no, I did not downvote. – Turing85 Oct 31 '17 at 20:11
  • Well, it's not Mockito, because the Mockito @Mock annotation can't go on a method like this. Never heard of a Java library called Mockit, but that doesn't mean it doesn't exist. And I can't comment on JMockit because I'm not expert in it. So I don't know how to answer this. – Dawood ibn Kareem Oct 31 '17 at 20:13
  • 1
    Looks to be JMockit: http://jmockit.org/api1x/mockit/MockUp.html oh look, I spent an **insignificant** time Googling this and instantly found relevant links. – Kayaman Oct 31 '17 at 20:15
  • Yeah, now that Jeff has added the imports, it's clearly JMockit, not Mockito. I'll fix up the tag. – Dawood ibn Kareem Oct 31 '17 at 20:17
  • Yes, if Mockit is a different library than Mockito, that would be good to know but when I search for Mockit of course I get links for Mockito. The pom shows dependencies for Mockito, PowerMock and Jmockit. – Jeff Oct 31 '17 at 20:17
  • Okay, I have been looking at the wrong documentation -- thanks, I will look at JMockit docs. – Jeff Oct 31 '17 at 20:18
  • Yeah, in my opinion, this is the fault of the makers of JMockit, for naming their classes `mockit.*`, instead of `org.jmockit.*`. Standards are there for a reason, and I consider this a bug. – Dawood ibn Kareem Oct 31 '17 at 20:20
  • Jeff, I would recommend you talk to your team leader about having only ONE Mocking framework in your project. Sure, JMockit and Mockito have slightly different features, but if you honestly need more than one way to mock things, then you're doing something wrong. It's possible that your team is trying to move off one framework onto the other, which is why you have both in your pom. It's almost certain that your team have an agreed preference for one or the other, and you should probably align your code to that preference. – Dawood ibn Kareem Oct 31 '17 at 21:20
  • @DawoodibnKareem That's not the real issue here (although "org.jmockit" *is* better and that's what JMockit 2 is going to use - also, JUnit 3.8 used the "junit" package, only starting with "org.junit" in JUnit 4). The real cause of the OP's difficulty seems to be his Java IDE skills: normally, you simply move the cursor over any API usage in code (for example, the `new MockUp` call) for any IDE to promptly show available API documentation. And if said API documentation is not available, then again we are back to basic IDE-using skills... – Rogério Nov 02 '17 at 19:53
  • Actually, the central question is what the purpose of the code I posted. Even after looking at the documentation, it is not clear whether the intention is to affect every instance of the class or what. But keep having this pointless conversation. This is probably the most condescending set of responses I have ever gotten here. – Jeff Nov 02 '17 at 20:48

1 Answers1

1

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"

fgb
  • 18,439
  • 2
  • 38
  • 52
  • Thanks and this was just what I was looking for and the posters who discussed my Eclipse skills were responding for unknown or pedantic reasons (I suspect the latter). – Jeff Nov 05 '17 at 14:56