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

[Feature Request] Ability to name mock objects #709

Closed
vaydoln opened this issue Apr 14, 2021 · 1 comment
Closed

[Feature Request] Ability to name mock objects #709

vaydoln opened this issue Apr 14, 2021 · 1 comment

Comments

@vaydoln
Copy link

vaydoln commented Apr 14, 2021

It would be extremely helpful to have the ability to "name" mock instances so that they would show up with that name in toString calls (or at least in the error messages for validations). It is not always possible to only have a single mock instance of a given type so having the ability to quickly see what happened in the error output would save a bit of time.

`
@test
void myTest(
@Injectable SomeClass arg1,
@Injectable SomeClass arg2
) {
MyTestClass testClass = new MyTestClass();

testClass.someMethod(arg1);
testClass.someMethod(arg2);

new FullVerifications({{
    arg1.expectedMethod(); times = 1;
    arg2.expectedMethod(); times = 1;
}});

}
`

[I am totally failing at the markdown, no idea why it won't format correctly]

If the validation fails because of an unexpected invocation or a missing invocation, then it's sometimes difficult to debug which one is the problem.

There might be a better way, but my spitball idea would be to add an optional parameter to the annotation:

@Injectable("arg1") SomeClass arg1

@rliesenfeld
Copy link
Member

rliesenfeld commented Apr 18, 2021

The name of a mock parameter, just like for mock fields, is already used in error messages, and in the matching of injection points to provided @Tested/@Injectable values. But what you ask for is something else, right? When a verification fails on arg1.expectedMethod(), the exception stack trace already points to the exact line in the test which failed, so we don't need the error message to mention "arg1".

Anyway, this would be costly to implement, and I don't really see the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants