我想实施一个责任链模式,处理“断链”问题如下:
public abstract class Handler{
private Handler m_successor;
public void setSuccessor(Handler successor)
{
m_successor = successor;
}
protected abstract boolean handleRequestImpl(Request request);
public final void handleRequest(Request request)
{
boolean handledByThisNode = this.handleRequestImpl(request);
if (m_successor != null && !handledByThisNode)
{
m_successor.handleRequest(request);
}
}
}
似乎是一种常见的方法。但是,如何使用受保护的抽象方法进行测试?处理这个问题的方法似乎是:
Handler
的仅测试子类。这对于测试维护来说似乎很糟糕。handleRequest
方法的单元测试。但这似乎不是组织测试的合理方式。我读过[1]这种测试问题意味着设计是错误的,并建议使用组合而不是继承。我现在正在尝试这个,但似乎很奇怪,这个模式的推荐实现有这个问题,但我找不到任何有关单元测试的建议。
更新: 我已经用如图所示的依赖性反转替换了抽象类,现在使用Mockito可以很容易地测试它。它仍然看起来像责任链...我错过了什么?
// Implement a concrete class instead
public class ChainLink {
// Successor as before, but with new class type
private ChainLink m_successor;
// New type, RequestHandler
private RequestHandler m_handler;
// Constructor, with RequestHandler injected
public ChainLink(RequestHandler m_handler) {
this.m_handler = m_handler;
}
// Setter as before, but with new class type
public void setSuccessor(ChainLink successor) {
m_successor = successor;
}
public final void handleRequest(Request request) {
boolean handledByThisNode = m_handler.handleRequest(request);
if (m_successor != null && !handledByThisNode) {
m_successor.handleRequest(request);
}
}
}
答案 0 :(得分:1)
如果您使用PowerMock + Mockito,您可以编写类似于此的测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Tests.class)
public class Tests {
@Test
public void testHandledByFirst() throws Exception {
Request req = ...;
Handler h1 = mock(Handler.class);
Handler h2 = mock(Handler.class);
when(h1, "setSuccessor", h2).thenCallRealMethod();
when(h1, "handleRequestImpl", req).thenReturn(true);
h1.setSuccessor(h2);
h1.handleRequest(req);
verify(h2, times(0)).handleRequest(req);
}
@Test
public void testHandledBySecond() throws Exception {
Request req = ...;
Handler h1 = mock(Handler.class);
Handler h2 = mock(Handler.class);
when(h1, "setSuccessor", h2).thenCallRealMethod();
when(h1, "handleRequestImpl", req).thenReturn(false);
h1.setSuccessor(h2);
h1.handleRequest(req);
verify(h2, times(1)).handleRequest(req);
}
}
当第一个处理程序返回false时会验证你的第二个处理程序的方法,并且当它返回true时不会调用它。
另一种选择是遵循众所周知的“赞成组合而不是继承”的规则,并将你的班级改为这样的:
public interface Callable {
public boolean call(Request request);
}
public class Handler {
private Callable thisCallable;
private Callable nextCallable;
public Handler(Callable thisCallable, Callable nextCallable) {
this.thisCallable = thisCallable;
this.nextCallable = nextCallable;
}
public boolean handle(Request request) {
return thisCallable.call(request)
|| (nextCallable != null && nextCallable.call(request));
}
}
然后你可以用这种方式模拟它(或者使用几乎任何模拟框架,因为你没有受保护的方法):
@RunWith(PowerMockRunner.class)
@PrepareForTest(Tests.class)
public class Tests {
@Test
public void testHandledByFirst() throws Exception {
Request req = ...;
Callable c1 = mock(Callable.class);
Callable c2 = mock(Callable.class);
Handler handler = new Handler(c1, c2);
when(c1.call(req)).thenReturn(true);
handler.handle(req);
verify(c1, times(1)).call(req);
verify(c2, times(0)).call(req);
}
@Test
public void testHandledBySecond() throws Exception {
Request req = ...;
Callable c1 = mock(Callable.class);
Callable c2 = mock(Callable.class);
Handler handler = new Handler(c1, c2);
when(c1.call(req)).thenReturn(false);
handler.handle(req);
verify(c1, times(1)).call(req);
verify(c2, times(1)).call(req);
}
}
在此解决方案中,您还可以在Callable之后继承Handler,然后您可以将其包装在任何其他可能具有后继的callable上,并使用处理程序而不是原始的callable;它更加灵活。
答案 1 :(得分:0)
我会去选项1.那个假子类很简单,你可以将它放在你的tets类中。仅测试子类没有任何问题。