Unit tests generation in Mapstruct

2,766 views
Skip to first unread message

Pooja Venkatram

unread,
Nov 12, 2016, 11:37:43 AM11/12/16
to mapstruct-users
Hi, 

As I am using Mapstruct for my project, I feel the need for a Unit Test generation just like how Mapstruct has Mapping Code generation. We should have a way of generating a Unit test for the class that was just mapped by Mapstruct. We can just do Asserts on the source and target fields that we mapped. The user would obviously have to set the values for the objects, but apart from that he should be able to run the code as is.  

Best
Pooja

Sjaak Derksen

unread,
Nov 14, 2016, 3:44:34 PM11/14/16
to mapstruct-users
Hi Pooja,

You might want to try assertReflectionEquals from: http://www.unitils.org/tutorial-reflectionassert.html in combination with reverse mapping. So: prepare an object(graph). Map it with MapStruct. Do a reverse map with MapStruct and run assertReflectionEquals. 

I use this roundtrip mapping all the time for mappings from JAXB to DTO and vice versa.

Best regards,
Sjaak

Pooja Venkatram

unread,
Nov 21, 2016, 7:32:32 PM11/21/16
to mapstruct-users
Hi Sjaak, 

Thank you so much for your response. I will take a look into this. the only problem with reverse mapping is that some of the objects are not reverse mapping compatible(like immutable fields). Do you deal with any such fields or are all your fields reverse mappable?

Best
Pooja

Sjaak Derksen

unread,
Dec 5, 2016, 2:56:10 PM12/5/16
to mapstruct-users
Hi Pooja,

Sorry for my late response. For immutable fields I don't have a direct solution.  You could however make the problem a bit smaller by setting them manual before you run reflectionAssertEquals and test those remaining ones with regular asserts on the target side.

Best regards,
Sjaak 
Reply all
Reply to author
Forward
0 new messages