Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating mock instances on demand (feature request) #75

Closed
borisbrodski opened this issue Sep 24, 2014 · 6 comments
Closed

Creating mock instances on demand (feature request) #75

borisbrodski opened this issue Sep 24, 2014 · 6 comments
Assignees
Labels

Comments

@borisbrodski
Copy link

@borisbrodski borisbrodski commented Sep 24, 2014

It would be really great, if there were some possibility to create mock objects dynamically on demand:

Suppose I have a class:

class MyLogic {
  MyLogic(<a lot of nasty parameters....>) {}
}

Then I what to partially mock some methods like this:

new NonStrictExpectations(MyLogic.class) {{
  Deencapsulation.newMockInstance(MyLogic.class).methodToMock(...);
}}

Background:
Actually I'm working on JMockit-Xtend bridge. In Xtend you can define mocks much more elegant as in Java. Check it out here: https://github.com/borisbrodski/jmockit-xtend

Especially I'm working on following partial mocking feature:

// Define non-strict expectation
stub(MyLogic) [ it, myLogicMock |
  myLogicMock.methodToMock(...)
]

stub defines a (non-strict) expectation block with partial mock MyLogic.class. In the second parameter you can get a mocked instance of the class, so you can just call methods on it. Isn't is cool?

Currently I'm using sun Reflection, but it can't instantiate abstract classes... But JMockit can! :)

Thank you in advance.

@rliesenfeld

This comment has been minimized.

Copy link
Member

@rliesenfeld rliesenfeld commented Sep 24, 2014

Ok, maybe we can do something.

One thing I considered in the past was adding a "Deencapsulation.newUninitializedInstance(Class)" method, which would simply create an instance of the given class without executing any constructor (something which JMockit already does internally, to create mock instances). It could also work for abstract classes, by first creating a subclass with empty implementations for any abstract methods.

Would this solve the question?

@borisbrodski

This comment has been minimized.

Copy link
Author

@borisbrodski borisbrodski commented Sep 25, 2014

Yes, this is exactly what I need! Also I like the name newUninitializedInstance much better, then newMockInstance.

If I can somehow help you with it, please, let me know.

@rliesenfeld rliesenfeld self-assigned this Sep 25, 2014
@rliesenfeld

This comment has been minimized.

Copy link
Member

@rliesenfeld rliesenfeld commented Sep 25, 2014

Ok, the method will be added for release 1.13.

@borisbrodski

This comment has been minimized.

Copy link
Author

@borisbrodski borisbrodski commented Sep 25, 2014

Thank you! Looking forward to test it 👍

@borisbrodski

This comment has been minimized.

Copy link
Author

@borisbrodski borisbrodski commented Sep 29, 2014

Cool, thank you!

Two questions: does new ConcreteSubclass<T>(classToInstantiate).generateClass() caches generated classes and shares it with the JMockit main engine? Should I cache it within my JMockit-Xtend bridge?

@rliesenfeld

This comment has been minimized.

Copy link
Member

@rliesenfeld rliesenfeld commented Sep 29, 2014

If I remember correctly, currently it does not cache the generated class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.