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

Dynamically create mock instance. #46

Closed
Dao007forever opened this issue Aug 15, 2014 · 3 comments
Closed

Dynamically create mock instance. #46

Dao007forever opened this issue Aug 15, 2014 · 3 comments
Labels

Comments

@Dao007forever
Copy link

Hi,

I know the @mocked is great, but is there any way we can create mock manually and use them in Expectations API? I mean I want to have a function to generate mock objects on demand while still able to create non-mocked objects. We have the @Injectable, why can't we create mock objects on demand?

@rliesenfeld
Copy link
Member

I am not sure what you're asking. When @mocked is used, any number of additional mocked instances can be created simply be instantiating them with a "new TheMockedClass(...)" expression. You can also use the "NonStrictExpectations(Object...)" constructor to turn non-mocked instances to mocked ones.

If your use case is different, I would need to see an example so I can understand it.

@Dao007forever
Copy link
Author

When @mocked are used, we can't create new instances of real object. What I was asking is something like dynamically create @Injectable instances.

private A[] createArray(int n, boolean mocked) {
  if (mocked) {
  } else {
  }
}

This function is handy in the case we parameterize the test functions. The "NonStrictExpectations(Object...)" can create partial mock why I want fully mocked object (this should be simpler, so I don't see why I can't do it.)

@rliesenfeld
Copy link
Member

Well, I've never seen a realistic situation where this would be useful. If that changes, the API may be changed to accomodate it.

Col-E pushed a commit to Col-E/jmockit1 that referenced this issue Oct 7, 2022
….plugins-maven-surefire-plugin-3.x

Update dependency org.apache.maven.plugins:maven-surefire-plugin to v3.0.0-M7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants