Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot inject MapStruct mapper into spring-boot JUnit test

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.

like image 458
user1927865 Avatar asked Jun 18 '18 16:06

user1927865


1 Answers

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})
like image 134
Makoto Avatar answered Oct 26 '22 01:10

Makoto