-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MapStruct Mapper Fails to Inject Dependencies When Lombok and Spring Are Used Without Mocks in Tests #28592
Comments
I think creating a mapper via new and using the generated MapperImpl is at least discouraged. You should create mappers via Mappers.getInstance but I suppose that's not working with spring injected beans into mappers. |
The default JHipster Mapper Tests are also generated like this. Lines 27 to 42 in e6d3824
Since we also adjusted our mappers to make use of this as we need a bit more complex objects:
we had to also adjust the corresponding tests manually ;-) e.g.:
|
@OmarHawk Interesting (the generated tests use the |
Ok, well, for us it is more important to not duplicate mapping code and having a consistent mapping for sub-entities, so injection of an already well-defined mapper for another Entity instead of having to write own code for that is beneficial to us. ;-) |
But you would not need spring injection for that (off topic discussion) when all mappers are not using any injection model |
I fixed the problem by injecting the mapper directly instead of the Impl using @Injectmocks and injecting required beans with @mock. @ExtendWith(MockitoExtension.class)
class UserSessionMapperTest {
@InjectMocks
private UserSessionMapper userSessionMapper;
@Mock
private GeoLocationMapper geoLocationMapper;
@Mock
private UserMapper userMapper;
@BeforeEach
void setUp() {
}
@Test
void shouldConvertToDtoAndBack() {
var expected = getUserSessionEntitySample1();
var actual = userSessionMapper.toEntity(userSessionMapper.toDto(expected));
assertUserSessionEntityAllPropertiesEquals(expected, actual);
}
} I don't know if its best practice or not |
@OmarHawk should we do anything related to this issue? |
Well, at least for avoiding code duplications for the MapStructExpression fields, this would be helpful. At the moment, that expression is duplicated into every mapper that accesses such a field, e.g. (manually typed here, hope there's no syntax error)
produces in Mapper class of NeedsSomeConfigA, NeedsSomeConfigB, NeedsSomeConfigC the very same expression String as it is already there in CentralConfigEntity. Also that generated mapper method in that very same Mapper class strips out every field from the other entity that is not needed from the frontend (usually just primary key & these expression fields are kept) and therefore wouldn't even consider the third level of nesting of such entities. But in our case we do need in some places deeply nested objects as a complete thing in our frontend, so we had to adjust the mappers as mentioned above and also implicitly got rid of these duplicated expressions there. |
Overview of the issue
When using MapStruct with Lombok in JHipster-generated mappers, tests fail with
NullPointerException
due to missing dependency injection (e.g.,UserActivityMapper
not being injected). This occurs in non-mocked tests where mappers depend on other mappers.Error Example:
[ERROR] ApiErrorMapperTest.shouldConvertToDtoAndBack:21 » NullPointer
Cannot invoke "UserActivityMapper.toDto(...)" because "this.userActivityMapper" is null
Motivation for or Use Case
This bug prevents testing mapper conversions without using Mockito mocks (
@Mock
/@InjectMocks
). It affects JHipster projects relying on Spring-managed dependency injection for MapStruct.Reproduce the error
ApiErrorMapper
usesUserActivityMapper
).The text was updated successfully, but these errors were encountered: