in Education by
In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or use the "real" method. The problem is, I can't seem to figure out how to call the real method from the mocked method. I found that you can define a special member within the mock object named "it" (with type of the object being mocked). This allows you to reference the real class from the mock implementation. So, my plan was, if I needed to invoke the "real" implementation of foo(), the mock method would call it.foo(). However, this doesn't work, because calling it.foo() just calls the mock version again, not the real version, so I end up with infinite recursion. Is there some way to make this work? EDIT: it might be clearer with a code example, here's what my current mocked method implementation looks like: private RealClass it; ... public SomeClass foo() { if(fakeIt) { return new SomeClass("fakevalue"); } else { // doesn't work, just keeps calling the mock foo // in infinite recursion return it.foo(); } } EDIT 2: Also, for most of my test cases I do NOT want the mock implementation. So my initial attempt at this was to only call Mockit.redefineMethods() within those test cases where I needed the mock object. But this didn't work - it seems you can only do this within setup/teardown ... my mock implementation never got called when I tried that. NOTES ON SOLUTION: At first I didn't think the answer given worked, but after playing with it some more, it seems the problem is that I was mixing JMockit "core" methods with the "annotation" driven methods. Apparently when using the annotation you need to use Mockit.setupMocks, not Mockit.redefineMethods(). This is what finally worked: @Before public void setUp() throws Exception { Mockit.setUpMocks(MyMockClass.class); } Then, for the mock class: @MockClass(realClass = RealClass.class) public static class MyMockClass { private static boolean fakeIt = false; private RealClass it; @Mock(reentrant = true) public SomeClass foo() { if(fakeIt) { return new SomeClass("fakevalue"); } else { return it.foo(); } } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In more recent versions of JMockit, Invocation.proceed() can be called from within a MockUp implementation. See Accessing the invocation context. public class MyMockClass extends MockUp { private static boolean fakeIt = false; @Mock public SomeClass foo(Invocation inv) { if (fakeIt) { return new SomeClass("fakevalue"); } else { return inv.proceed(); } } }

Related questions

0 votes
    I would like to call a .net 4.0 dll from an IIS7 application that is running in a .net 2.0 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am trying to call a method from the script Dice and after that method is executed the value of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    What is the easiest way to call a web service from Excel (I am using version 2002)? Please, no ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    What is the easiest way to call a web service from Excel (I am using version 2002)? Please, no ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    How to deep dive into the execution of a method from a method call? (a) F3 (b) F5 (c) F7 (d) ... Server Technologies & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    How to deep dive into the execution of a method from a method call? (a) F3 (b) F5 (c) ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    How can you call a controller method from JavaScript?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    The ________ for R are the main feature that make it different from the original S language. (a) scoping ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Original idea comes of Literate Statistical Practice from _______________ (a) Don Knuth (b) Don Cutting (c) ... and answers pdf, Data Science interview questions for beginners...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
...