5

I'm trying to write a unit test for a MapStruct mapper with componentModel="spring".

The application works perfectly, including the mapper injection. The problem is that the mapper is not injected to the test class and I'm getting the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.onap.sdc.workflow.api.mapping.WorkflowMapperTest': Unsatisfied dependency expressed through field 'workflowMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.onap.sdc.workflow.services.mappers.WorkflowMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I'm using intellij IDEA and target\generated-sources is marked.

Here is the mapper class:

@Mapper(componentModel = "spring")
public interface WorkflowMapper {

@Mapping(source = "properties", target = "category", qualifiedByName = "propertiesToCategoryMapper")
Workflow itemToWorkflow(Item item);

@Mapping(source = "category", target = "properties", qualifiedByName = "categoryToPropertiesMapper")
@InheritInverseConfiguration
Item workflowToItem(Workflow workflow);

@Named("propertiesToCategoryMapper")
default String customPropertiesToCategoryMapper(Map<String, Object> properties) {
    return String.class.cast(properties.get(WorkflowProperty.CATEGORY));
}

@Named("categoryToPropertiesMapper")
default Map<String, Object> customCategoryToPropertiesMapper(String category) {
    return Collections.singletonMap(WorkflowProperty.CATEGORY, category);
}

I'm using this mapper in the following code snippet:

@Service("workflowManager")
public class WorkflowManagerImpl implements WorkflowManager {

private WorkflowMapper workflowMapper;

@Autowired
public WorkflowManagerImpl(WorkflowMapper workflowMapper) {
    this.workflowMapper = workflowMapper;
}

...some code

The unit test class:

@ContextConfiguration(classes = 
WorkflowMapperTest.WorkflowMapperSpringTestConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class WorkflowMapperTest {

@Configuration
@ComponentScan(basePackageClasses = WorkflowMapperTest.class)
public static class WorkflowMapperSpringTestConfig { }

@Autowired
WorkflowMapper workflowMapper;

@Test
public void shouldMapItemPropertyToWorkflowCategory() {
    ...some code...
}

Any help will be appreciated.

1 Answer 1

9

At a glance, you don't include the bean you wish to test as part of your component scan.

You'd want to update your @ComponentScan configuration to include it.

@ComponentScan(basePackageClasses = {WorkflowMapperTest.class,
                                     WorkflowMapper.class,
                                     WorkflowMapperImpl.class})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I just added WorkflowMapper.class and it did the work. Why should I add the WorkflowMapperImpl.class? do you think I need it too?
If you are not changing the package of your generated mapper then including only the WorkflowMapper is sufficient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.