0

I am trying to mock a generic interface resource that has multiple options in one class. I found something that gets passed the named resources, that being to name the variable the same as the name parameter on the Resource. But if I have two @Injectable fields that have the same name (but different generic types) I cannot do this for both. Is there a way around this?

@Service("SourceDescriptionService")
public class SourceDescriptionServiceImpl implements CrudService<LnssDescription> {
    @Resource(name = "LnssCrudRepository")
    private LnssCrudRepository<LnssDescription> descriptionRepository;

    @Resource(name = "SourceRepository")
    private SourceRepository sourceRepository;

    @Resource(name = "LnssCruRepository")
    private LnssCruRepository<LnssState> stateRepository;

    @Resource(name = "LnssCruRepository")
    private LnssCruRepository<LnssVariant> variantRepository;

And my attempt at testing:

@Tested
private SourceDescriptionServiceImpl sut;

@Injectable
private SourceRepository SourceRepository;

@Injectable
private LnssCrudRepository<LnssDescription> LnssCrudRepository;

@Injectable
private LnssCruRepository<?> LnssCruRepository;

I've tried other variations on the CruRepository interface like parameterizing the LnssState, which then gives LnssVariant as the error or removing the parameterization which just goes back to the state being the error.

This is the error:

java.lang.IllegalStateException: Missing @Injectable for field "com.ln.sourceset.model.repository.LnssCruRepository<com.ln.sourceset.model.data.LnssState> stateRepository" in SourceDescriptionServiceImpl

Any help would be greatly appreciated! Thanks.

Mimerr
  • 390
  • 1
  • 5
  • 14
  • The mock field has type `LnssCruRepository>`, which is not the same as either `LnssCruRepository` or `LnssCruRepository`. If the corresponding fields in `SourceDescriptionServiceImpl` are declared as type `LnssCruRepository>`, then it works. But the real problem here is that both target fields use the *same* `@Resource` name. I always thought this wasn't valid, but even then, what is the sense in injecting the same resource when they have different types? – Rogério Oct 21 '16 at 17:28
  • Sorry, yes that was just one thing I tried. Also tried with LnssCruRepository and LnssCruRepository in the test class as injectables. But having both I couldn't name both variables LnssCruRepository. So yes, that is the real problem. I found a PR on the JMockit github that mentioned giving the @Injectable a (name = x) option but was closed, instead siting the variable name thing. So is having more than one resource with one name something that just isn't supported? (It is technically valid in Spring even if it doesn't make sense?) – Mimerr Oct 21 '16 at 19:23
  • Ok. Does the `SourceDescriptionServiceImpl` class actually needs to have two injected `@Resource` objects of different types but with the same name? Why can't these simply be two resources with different names? – Rogério Oct 21 '16 at 20:50
  • I didn't actually write this. I'm just pushing that we use JMockit as our mocking framework for unit tests and someone was having trouble with this. After looking further into it, I believe we are just going to delete the (name = x) parts because they do not seem to actually be needed at all in our implementation. Thanks for taking a look. – Mimerr Oct 24 '16 at 12:36

1 Answers1

0

Have your test Injectable name same as your dependency in the class you are testing.

Class you are testing has following dependency

@Inject
@Named("somename")
private SomeClass somename;

Injectable in your test case testing above class need to have below.

@Injectable
private SomeClass somename;

Hope this helps

Anil Konduru
  • 878
  • 10
  • 18