I have created a new class which extends a 3rd party abstract class. The new class calls methods in the abstract class. The problem I have is when trying to write the unit test, I'm not sure how to write a test as I would not know the exact details the 3rd party class requires.
The AbstractDecoratorMapper below is a SiteMesh specific class which I have to extend for SiteMesh to work correctly. As far as I can tell from the documentation I can't use composition.
public final class PartnerDecoratorMapper extends AbstractDecoratorMapper {
@Override
public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
super.init(config, properties, parent);
}
@Override
public Decorator getDecorator(HttpServletRequest request, Page page) {
if (super.getDecorator(request, page).getName().equalsIgnoreCase("default")) {
return getNamedDecorator(request, "externalPartnerDefault");
}
return super.getDecorator(request, page);
}
}
I use JMock if there is anything this tool can do to help.
There are two things you might be asking. I'll try to answer both.
Possible Question 1) How do I test my Additional Functionality?
Possible Question 2) How do I test the the combined functionality is appropriate?
Let me start with Question1.
I would suggest a simple peel. There is a video of the technique here: http://www.youtube.com/watch?v=p0tILwRZH5Q (the video is c# but basically identical in java)
which would mean you would have the following code afterwards
@Override
public Decorator getDecorator(HttpServletRequest request, Page page) {
Decorator d = super.getDecorator(request, page);
return getResolvedDecorator(d, d.getName(), request);
}
public Decorator getResolvedDecorator(Decorator current, String name, HttpServletRequest request) {
if (name.equalsIgnoreCase("default")) {
return getNamedDecorator(request, "externalPartnerDefault");
}
return current;
}
Now you can test this with a call like
assertEquals(expected, new PartnerDecoratorMapper().getResolvedDecorator(null, "default", MockHttpServletRequest);
I would suggest possibly removing data from HttpServletRequest as well, to make the testing & intent clearer.
Note: this has the added performance benefit that the call to super.getDecorator() only occurs once, since the result is cached.
Additional Note: It is also worth noting that the override of init() is unneeded, and doesn't actually do anything.
Question 2: This is a harder question, as you don't state what the desired behavior is. I am assuming this is a factory pattern, so most likely it would work in some manner like
| http request quality 1 | Page quality 1| Expected Decorator | | /mypath/mypage | status = foo | MyDecorator |
(I have no idea what this above chart should contain or look like) Once you have this behavior then the test to ensure it would be rather straight forward.
Happy Testing, Llewellyn