How to stub/mock XMLBeans object for testing?
-
23-06-2021 - |
Pergunta
I'm working with an application using Apache XMLBeans (interfaces for XML structure elements extending org.apache.xmlbeans.XmlObject, implementations for those interfaces extending org.apache.xmlbeans.impl.values.XmlComplexContentImpl)
I got following kind of functionality:
- An XML object is passed to a method with some data
- Based on this data, subcontent is generated for this XML object
- using XML objects' addFoo() to add elements etc
Now I'd like to have a test for this functionality but I'm not sure what is the best way to have this XML object for testing (I'd like the stub/mock object to maintain the subtree state to be easily tested). Some options:
- I tried instantiating these FooImpl implementation objects but they require reference to org.apache.xmlbeans.SchemaType sType
- Is there some way to instantiate these?
- Giving null / mocked SchemaType ends up with NPE when something is added
- Generating a stub classes with the few needed methods would work but ends up with many (> 100) methods to generate
- Using Mockito works otherwise, but to have the xml tree structure state easily tested, I need to use doAnswer() etc
Some ideas?
br, Touko
Solução
Found myself that the answer was pretty easy : The xmlbeans interfaces have factory classes for instantiating the beans, for example:
Address.Factory.newInstance()
Outras dicas
I know that this is not the answer you're looking but here it goes anyway:
Don't mock types you don't control
References:
- Steve Freeman and Nat Price from Growing Object Oriented Systems, Guided by Tests
- Steve Freeman blog post about this subject: http://stevef.truemesh.com/archives/000194.html
- Detailed experience from a developer: http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html
A question for you: What are you trying to test? That Apache XML beans works as expected? Or are you writing an XML tool?