0

So I have this class I'm testing. It's a helper that pulls stuff out of some XML classes. The XML classes are kind of a "Russian Doll" arrangement where you have to dig through many levels to get the right thing you need. Problem is that the class under test calls many of it's own methods. This makes it pretty hard to test without a bunch of redundancy. This is to say that I don't want to retest any of those "low hanging fruit" methods that retrieve objects from shallow locations if I can avoid it. I smell an opportunity for a re-factor here but I'm unsure what it should be. Note that there are often 6 and 7 levels to our "Russian Doll". My intuition says that I should maybe refactor the class under test to a hierarchy of classes, roughly one for each level in our "Russian Doll". Then maybe I could test one at a time without redundancy.

Questions: What are the patterns I should be looking at for this? Is there an approach for doing this which is Composition rather than Inheritance based?

user447607
  • 5,149
  • 13
  • 33
  • 55
  • Thinking about it some more, I think I need to check all of the methods and see which are actually used externally. Those that are not can become methods in a wired-in component. – user447607 Sep 29 '15 at 13:32

1 Answers1

0

without seeing the class its hard to give you an advise on how to test it. I assume that you're talking about a java class isn't it ? And you use the JAXB framework or a similar XML binding Framework. Regarding this, I would say that you don't want to test if the framework works right, but you want to test if you get the "right thing" out your russian doll. I would just write a test that tests if you get the stuff you're looking for without testing the methods in-between ... if they just use the xml binding framework to unwrap stuff, then trust the framework. If not you could also mock the methods in-between. Then you could also test them one by one without redundancy.

MeBNoah
  • 165
  • 14
  • JAXB. Yes. Yes, it's a Java class. I've been mocking the Object.getValue().getNextObject().getValue()... calls. IDK if the path is important though. This is to say that maybe there are two instances of the same type of object in different locations... where the difference is contextual. I'm wondering if there isn't a tool for this kind of thing in the Apache commons. For example you might provide a path and it grabs what you want. – user447607 Sep 29 '15 at 13:49
  • Still hard to follow without seeing the actual code ... have a look at http://stackoverflow.com/questions/17141154/unmarshalling-an-xml-using-xpath-expression-and-jaxb perhaps this helps. – MeBNoah Sep 29 '15 at 14:00
  • 1
    https://commons.apache.org/proper/commons-jxpath/users-guide.html I think this is the answer to my problem. – user447607 Sep 29 '15 at 14:36
  • 1
    I was wrong. JXPath does nested iteration to find elements. Nasty. – user447607 Oct 07 '15 at 15:15