0

My understanding of "best practices" when using an automatic mapping tool like ModelMapper is to have unit tests for any source->destination map that occurs in your code, to be sure the mapping is doing what you expect.

Is there any code coverage/static analysis tools that would assist in determining that for every invocation of ModelMapper#map source/destination pair, there exists a unit test that invokes ModelMapper#map with the same source/destination types?

Pete
  • 11,313
  • 4
  • 43
  • 54
  • There is probably nothing readymade. However, you could parse the Byte code of any class of your project and check for such invocations from your unit tests. I would however not recommend this, rather make it explicit by a dedicated parametic test. – Rafael Winterhalter Aug 07 '14 at 14:40
  • @raphw I am writing such tests now, but I am hoping for a way to verify that I didn't skip writing a test in haste by verifying a test exists for any map call in the code. – Pete Aug 07 '14 at 14:52
  • Then you would need the above manual verification. But why do you need this feature for so many classes in the first place? Finding such errors is normally a job for a type system. You should avoid dynamic type mapping whereever applicable. – Rafael Winterhalter Aug 07 '14 at 14:55
  • I have several situations requiring mapping: mapping domain classes yo DTOs for serialization in HTTP responses, and mapping equivalent classes from a set of web services with effectively identical classes, but different packages. – Pete Aug 07 '14 at 15:00
  • I have had the same issue plenty of times and I feel your pain. However, I found it better to refactor my classes in order to make such mapping obsolete whenever possible. Otherwise, I would recommend you to maybe add annotations to your classes (some `@MapsTo` for example and scan the entire application for such classes. Then, I would write a single test that verifies that any such mapping is tested which you could check by a naming convention for your test cases. – Rafael Winterhalter Aug 07 '14 at 15:04

0 Answers0