@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.
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:
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?