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

Add support for @javax.inject.Named and Spring's @Qualifier #214

rliesenfeld opened this issue Oct 9, 2015 · 4 comments

Add support for @javax.inject.Named and Spring's @Qualifier #214

rliesenfeld opened this issue Oct 9, 2015 · 4 comments


Copy link

@rliesenfeld rliesenfeld commented Oct 9, 2015

Occasionaly, a tested class may have dependencies of different types which, in turn, have some other dependency of a type X, which need to be resolved to different instances of type X.

When using CDI or Spring DI, these X dependencies are usually qualified so they can be properly resolved. CDI's @nAmed, and Spring's @qualifier annotations are then used on fields of type X to specify the name of the desired instance.

So, when an injected field is annotated with @nAmed("name") or @qualifier("name"), JMockit should look for an @Injectable or @tested instance of the same type and "name", instead of using the regular searching rules (just the type, or type + injected field name).

Copy link

@pith pith commented Mar 29, 2016

I'm using Guice and the support for @Named doesn't seem to work. My tested class has the following field:

@Inject @Named("git")
private FetchService fetchService;

When I mock it using @Injectable:

private FetchService fetchService;

I have the following error:

java.lang.IllegalStateException: Missing @Injectable for field MyTestedService#fetchService, of type org.mycompany.MyTestedService

If I remove the @Named("git") annotation my fetchService is mocked correctly.

Copy link

@codecounselor codecounselor commented Oct 1, 2016

I was working with a colleague recently and we encountered a side effect of this implementation.

The application under test has beans with qualifiers containing dashes. Since this code expects the injectable variable to match the beanId we were in a bit of a pickle.

We worked around it by changing the @Qualifier annotation to @Resource but that may not always be a viable option, and it feels like a hack.

My question is: why not add an attribute to the @Injectable annotation that specifies the bean name in the scenario there are multiple auto-wire candidates?

I haven't dug into the code yet, but if you'll accept a PR to remedy this issue I'd be willing to try and make the contribution myself.

Copy link

@nsirbu nsirbu commented Jul 16, 2020

So, how shall this be used?
Is this the correct usage? It doesn't seem to work.

private FetchService fetchService;

Copy link
Member Author

@rliesenfeld rliesenfeld commented Jul 16, 2020

This is covered in the documentation for @Tested.

@jmockit jmockit locked and limited conversation to collaborators Jul 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

No branches or pull requests

4 participants