I'm using MapStruct, mapstruct-jdk8 version 1.1.0.Final and defining abstract class that I inject via Spring.
I'm looking at how to be able to test them via Junit Test ? I've basicaly a main mapper that will use 2 sub mappers
@Mapper(componentModel = "spring", uses = {SubMapper1.class, SubMapper2.class})
public abstract class MainMapper {
@Mapping(target = "field1", qualifiedByName = {"MyMapper2Name", "toEntity"})
public abstract MyEntity toEntity(MyDto pDto);
public MyDto fromEntity(MyEntity pEntity) {
// Specific code, hence why I use Abstract class instead of interface.
}
}
I've tried several things but can't get the mapper to be instancied correctly to test it.
@RunWith(SpringRunner.class)
public class MainMapperTest {
private MainMapper service = Mappers.getMapper(MainMapper.class);
@Test
public void testToEntity() throws Exception {
.....
java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.mappers.MainMapper
I've also tried via @InjectMock but no dice either.
Cannot instantiate @InjectMocks field named 'service'. You haven't provided the instance at field declaration so I tried to construct the instance. However, I failed because: the type 'MainMapper is an abstract class.
And via Spring @Autowired
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mappers.MainMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I'm guessing this might have to do with annotation processor, and mapper not being generated when I launch test. I found this class as example.
However the class AnnotationProcessorTestRunner doesn't seems to be available before 1.2 which has no final release yet.
So my question is how do I write Junit tests to test my mapstruct abstract class mapper that I use via Spring injection in my code.
Assumming that:
MainMapper
mapper gets injected into@Component ConverterUsingMainMapper
You can use the following example:
In response to @Richard Lewan comment here is how I declared my test class for the abstract class ConfigurationMapper using 2 subMappers
You use the impl generated in the
SpringBootTest
annotation And then inject the class you want to testLet me know if you need more info, but from there it's straightforward. I didn't mock the subMapper, as it was better for me to test all the mapping process at once.
You are having multiple issues:
Mappers#getMapper(Class)
only with the defaultcomponentModel
, otherwise the mapper will not be instantiated correctly. If you are getting theRuntimeException
there it means that the implementation class was not generated. Make sure that you have a correct setupMainMapperImpl
and not against the abstract class.ComponentScan
and make sure that the implementation and the used mappers can be autowired.The class you linked is a wrong test class and is not related to your test case. Have a look at this integration test case for spring integration.
The
AnnotationProcessorTestRunner
is part of our tests and is used to test the annotation processor and has been there since the beginning. It is not part of the releases.