Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow injection of mocks of the same type #392

@lemano

Description

@lemano

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
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions