guhilling / cdi-test

JUnit extension for easy and efficient testing of CDI components
http://guhilling.github.io/cdi-test/
Apache License 2.0
25 stars 4 forks source link

Allow injection of mocks of the same type #392

Open lemano opened 8 months ago

lemano commented 8 months ago

Hello.

I'm trying to replace cdi-unit by cdi-test. I already managed to make it work for several cases, however I hit in a situation where I need several mocks of the same type on the same test. Due to a validation in cdi-test we can't inject mocks with the same type.

I would like that we use another attribute as a discriminator, allowing several mocks of the same type to be injected. Maybe mocks with a given name, could be injected with @Named or a qualifier with the same name as the mock.

No workaround came to my mind.

The code where it is blocking the injection:

    @Override
    public synchronized void onMockCreated(Object mock, MockCreationSettings settings) {
        final Class<?> typeToMock = settings.getTypeToMock();
        final Map<Class<?>, Object> mocks = currentMockSet();
        if (mocks.containsKey(typeToMock)) {
            throw new CdiTestException("mock " + typeToMock + " already in set");
        }
        try {
            mocks.put(typeToMock, mock);
        } catch (UnsupportedOperationException uoe) {
            // IGNORE
        }
    }
guhilling commented 8 months ago

I'll think about it. Could you create a test case for this?