Empty Autowired list of interface implementations

Hi,
I have this working piece of code:

@Component
public class CommandManagerFactory
{
	private final List<CommandManager> managerList;
	
	@Autowired
	public CommandManagerFactory(List<CommandManager> managerList) {
		this.managerList = managerList;
	}

	public CommandManager get(CommandType type) throws B2BServiceException
	{
		if (type == null)
			throw new B2BServiceException(ResultCode.BAD_REQUEST, "The type parameter can't be null");

		CommandManager manager = null;
		for (CommandManager mng : managerList)
		{
			if (type.equals(mng.getType()))
			{
				manager = mng;
				break;
			}
		}

		if (manager == null)
			throw new B2BServiceException(ResultCode.NOT_IMPLEMENTED, "The requested manager isn't implmented");

		return manager;
	}
}

But Cover returns R013 because the manager is null and the only option is because the array of the interface implementation is empty. So it seems that the autowiring is not working as expected.

Any idea on this?
Thank you!

Hello @Danilo_Petrone and thank you for getting in touch.
I’ve tried this example, I needed to add some dummy classes for CommandManager, CommandType, and B2BServiceException because I don’t know their content (and it shouldn’t matter too much). I do get some tests with Cover on the example:

@ContextConfiguration(classes = {CommandManagerFactory.class})
@ExtendWith(SpringExtension.class)
class CommandManagerFactoryTest {
  @MockBean
  private CommandManager commandManager;

  @Autowired
  private CommandManagerFactory commandManagerFactory;

  @Autowired
  private List<CommandManager> list;

  @Test
  void testGet() throws B2BServiceException {
    // Arrange
    when(this.commandManager.getType()).thenReturn(new CommandType());

    // Act and Assert
    assertThrows(B2BServiceException.class, () -> this.commandManagerFactory.get(new CommandType()));
    verify(this.commandManager).getType();
  }

  @Test
  void testGet2() throws B2BServiceException {
    // Arrange
    when(this.commandManager.getType()).thenReturn(new CommandType());

    // Act and Assert
    assertThrows(B2BServiceException.class, () -> this.commandManagerFactory.get(null));
  }
}

Although they all produce an exception, the autowiring seems to work fine for that case, we just create on CommandManager, which is a mock, to autowire it in the list.
Maybe there is a difference in configuration. Could you give more details about the dependencies of the project, what’s in the pom file? In particular, do you have spring-boot-test?
Otherwise the problem could be related to the CommandManager class. Do you otherwise get tests that mock the CommandManager class?