Reputation: 3062
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.
Upvotes: 58
Views: 99447
Reputation: 4802
Addition to @TheBakker's answer: as a lighter alternative to @SpringBootTest
you can use @ContextConfiguration
, if you do not require the whole SpringBoot stack. A example would look like this (based on code from original question):
@ExtendWith(SpringExtension.class) // JUnit 5
@ContextConfiguration(classes = {
MainMapperImpl.class,
SubMapper1Impl.class,
SubMapper2Impl.class
})
public class MainMapperTest {
[...]
With JUnit 4 use annotation RunWith
instead of ExtendWith
:
@RunWith(SpringRunner.class) // JUnit 4
[...]
MainMapperImpl.class
come from:MapStruct generates a class which extends public abstract class MainMapper
(see code in original question), or implements public interface MainMapper
(if you use interfaces for your mapper definition). This generated class has the same package as your mapping definition class/interface, and is named <mapper-name>Impl
.
Therefore: com.companyx.MainMapper
=> com.companyx.MainMapperImpl
.
You can inspect the code generated by MapStruct, it's usually in target/generated-sources/
, and sometimes very helpful for understanding what happens.
Upvotes: 36
Reputation: 316
For a complete unit test, without using spring contexts or mocks, you can do something like this:
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
uses = {SecondMapper.class, ThirdMapper.class},
injectionStrategy = InjectionStrategy.CONSTRUCTOR
)
public interface MyMapper {
SecondMapper and ThirdMapper are used in MyMapper. By setting injectionStrategy = InjectionStrategy.CONSTRUCTOR
the generated mapper will have a full argument constructor, something like this:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-04-04T16:28:14+0200",
comments = "version: 1.5.3.Final, compiler: javac, environment: Java 21.0.1 (Eclipse Adoptium)"
)
@Component
public class MyMapperImpl implements MyMapper {
private final SecondMapper secondMapper;
private final ThirdMapper thirdMapper;
@Autowired
public MyMapperImpl(SecondMapper secondMapper, ThirdMapper thirdMapper) {
this.secondMapper = secondMapper;
this.thirdMapper = thirdMapper;
}
// other generated code
}
Finally in your test you can define an instance of MyMapper constructed with instances of secondMapper and thirdMapper.
class MyMapperTest {
private final SecondMapper secondMapper = new SecondMapperImpl();
private final ThirdMapper thirdMapper = new ThirdMapperImpl();
private final MyMapper myMapper = new MyMapperImpl(secondMapper, thirdMapper);
}
No spring contexts. No mocks. Pure unit testing.
Upvotes: 1
Reputation: 2622
For anybody willing to solve this problem without running the test within Spring:
You can use Mockito's @InjectMock
annotation instead. Assuming your mapper is FooBarMapper
, relying onto subMapper QixSubMapper
and you are testing a service FooBarService
which use FooBarMapper
, you will write something like this:
@ExtendWith(MockitoExtension.class)
public class FooBarServiceTest {
FooBarService fooBarService;
@Mock QixSubMapper qixSubMapper;
@InjectMocks FooBarMapperImpl fooBarMapper;
@BeforeEach
void setup() {
fooBarService = new FooBarService(fooBarMapper);
Mockito.when(qixSubMapper.convert(Mockito.anyString())).thenCallRealMethod();
}
}
Here is how it works: you declare your Mapper with @InjectMock
, and you provided a mock for the submapper dependency, so when Junit will initialize the class with Mockito, it will inject the mock of the subMapper into the main Mapper.
Then all you have to do is to configure what the mock of the subMapper should do. In my case I just told to use the real method, so basically the real mappers are called but I used the @InjectMock
magic to make it seems like the @Autowired
worked
Upvotes: 1
Reputation: 385
In order to mapstruct you will use below
Mappers.getMappers(Your Mapper Class here.class)
E.g:-
OrderMapper INSTANCE = Mappers.getMapper( OrderMapper.class );
Use the INSTANCE Variable and access your mapper class methods here.
And then run with SpringRunner.class
@RunWith(SpringRunner.class)
If you find issue with implants not found or class not found on the Test class which is being tested. Then do maven clean test like below.
mvn clean test
Then you should be fine with your testing.
Reference:-
https://mapstruct.org/development/testing-mapstruct/
Upvotes: 0
Reputation: 9
It works One hundred percent !
@SpringBootTest
public class CustomMapperTest {
@Spy
private CustomMapper mapper = Mappers.getMapper(CustomMapper.class);
@Test
public void SCENARIO_CONVERT_ACTIVITY() {
ActivityEntity dbObj = mapper.toDto(ActivityDTO);
Assert.assertNotNull(dbObj);
Assert.assertEquals("XXXX", dbobj.getId());
}
@Mapper
public abstract Class CustomMapper{
@Mapping(source="activityDto.fathername",target="surname")
public abstract ActivityEntity toDto(ActivityDTO activityDto);
}
Upvotes: 0
Reputation: 179
You can also manually create an application context with your mapper and its collaborators:
ApplicationContext context = new AnnotationConfigApplicationContext(MainMapperImpl.class, SubMapper1Impl.class, SubMapper2Impl.class);
or
ApplicationContext context = new AnnotationConfigApplicationContext("package.name.for.your.mappers");
Then get your Mapper from context
MainMapper mainMapper = context.getBean(MainMapper.class);
Upvotes: 1
Reputation: 765
Using Mockito:
@Spy
private EntityMapper entityMapper = Mappers.getMapper(MyMapper.class);
And remeber to injects mocks in your class under test by, for example:
@InjectMocks
private MyClassUnderTest myClassUnderTest
Upvotes: 28
Reputation: 3062
In response to @Richard Lewan comment here is how I declared my test class for the abstract class ConfigurationMapper using 2 subMappers
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ConfigurationMapperImpl.class, SubMapper1Impl.class, SubMapper2Impl.class})
public class ConfigurationMapperTest {
You use the Impl
generated classes in the SpringBootTest
annotation and then inject the class you want to test:
@Autowired
private ConfigurationMapper configurationMapper;
Let 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.
Upvotes: 43
Reputation: 10505
Assumming that:
MainMapper
mapper gets injected into @Component ConverterUsingMainMapper
You can use the following example:
@RunWith(SpringRunner.class)
@ContextConfiguration
public class ConsentConverterTest {
@Autowired
MainMapper MainMapper;
@Autowired
ConverterUsingMainMapper converter;
@Configuration
public static class Config {
@Bean
public ConverterUsingMainMapper converterUsingMainMapper() {
return new ConverterUsingMainMapper();
}
@Bean
public MainMapper mainMapper() {
return Mappers.getMapper(MainMapper.class);
}
}
@Test
public void test1() {
// ... your test.
}
}
Upvotes: 6
Reputation: 21451
You are having multiple issues:
Mappers#getMapper(Class)
only with the default componentModel
, otherwise the mapper will not be instantiated correctly. If you are getting the RuntimeException
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.
Upvotes: 7