我想單元測試現有的ZK控制器,我想找到一種方法來處理調用諸如以下,而單元測試我控制器,我如何設置ZK會話(org.zkoss.zk.ui),以便我可以單元測試控制器類擴展GenericForwardComposer
Sessions.getCurrent().setAttribute("from", from.getValue());
我很樂意要麼更換有問題的代碼,或者找到變通的辦法進行單元測試。我的目標是通過與NullPointerException
我的測試是簡單的可測試性處理(這不是太糟糕一開始的地方......)控制器的
@Test
public void zkControllerDoesMockingInitialisedSuccessfully() throws Exception {
T2TripBigDaoInterface tripBigDao = createMock(T2TripBigDao.class);
ZkFieldValidator fieldValidator = createMock(ZkTextFieldValidator.class);
FieldRangeValidator rangeValidator = createMock(DefaultFieldRangeValidator.class);
TripController controller = new TripController(tripBigDao, fieldValidator, rangeValidator);
replay(tripBigDao, fieldValidator, rangeValidator);
controller.onClick$getTrips(new Event("getTrips"));
verify(tripBigDao, fieldValidator, rangeValidator);
// Test purpose: Just get a unit test of the controller running to start with....
}
提取物:
public class TripController extends GenericForwardComposer {
....
public void onClick$getTrips(Event event) throws Exception {
Sessions.getCurrent().setAttribute("from", from.getValue());
Sessions.getCurrent().setAttribute("to", to.getValue());
....
}
....
提取的堆棧跟蹤:
java.lang.NullPointerException
at com.t2.webservice.controller.alert.TripController.onClick$getTrips(TripController.java:72)
at com.t2.webservice.controller.alert.TripControllerTest.zkControllerDoesMockingInitialisedSuccessfully(TripControllerTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)