Search code examples
javaunit-testingjaxbxjc

jaxb xjc - how to generate unit test files


One of my project https://github.com/maithilish/gotz uses JAXB and to generate classes from schema it uses XJC along with maven-jaxb2-plugin. As the generated classes are basic POJO, is it possible to generate unit test cases for generated classes either through xjc or jaxb plugin.


Solution

  • Author of maven-jaxb2-plugin here.

    There is no such feature in maven-jaxb2-plugin and, frankly, there probably will never be. I cannot really imagine, what you would like to achieve with such unit tests. XJC-generated schema-derived classes are normally dumb as stones, there's literally nothing to test there. Generating tests just "for code coverage" is not reasonable from my point of view.

    However, I have one hint you may find helpful. What I do sometimes is so-called "roundtrip" tests where I take some sample XML, unmarshal it then marshal it an compare to the original XML. JAXB2 Basics can generate nice deep equals methods if you want to compare object structures instead.

    Roundtrip tests make some sense if you want to make sure your schema-derived classes match your XML. But you really don't need ane unit test generation for this.