Hello其他程序员。我查看过几十个"建议"在我决定在这里发布之前,互联网上的补救措施(包括在stackoverflow上)。但无济于事。
因此。我在JSF开始了一个小项目。在使用JSFUnit和Apache Cactus(这是另一个悲伤的故事)未能测试UIComponents和Managed Beans之间的关系之后,我希望能够至少测试域模型单元。唉。
我有这个POJO:
public class AppointmentFormBean implements Serializable, Observed{
private Date selectedDate;
private ArrayList<VGObserver> observers;
/**
* Creates a new instance of AppointmentFormBean
*/
public AppointmentFormBean() {
observers = new ArrayList<>();
}
public Date getSelectedDate() {
return selectedDate;
}
public void setSelectedDate(Date selectedDate) {
this.selectedDate = selectedDate;
notifyVGObservers();
}
@Override
public void registerVGObserver(VGObserver o) {
this.observers.add(o);
}
@Override
public void removeVGObserver(VGObserver o) {
int i = observers.indexOf(o);
if(i>=0)
observers.remove(o);
}
@Override
public void notifyVGObservers() {
observers.stream().map((observer1) -> (VGObserver) observer1).forEach((observer) -> {
observer.update(selectedDate);
});
}
}
(我故意省略了JSF注释 - 它们与我运行的测试无关)
此POJO实现的Observed接口如下所示:
public interface Observed {
public void registerVGObserver(VGObserver o);
public void removeVGObserver(VGObserver o);
public void notifyVGObservers();
}
Yip,从Head First Design Patterns复制而来,他们已经阅读过了: - )
VGObserver,如上所述 - 是另一个界面,它会&#34;听&#34;实现Observed的类中的更改:
public interface VGObserver {
public void update(Date selectedDate);
}
我对此设计有很大的计划,因为最终VGObserver(实现它的具体类)将尝试检索REST资源 - 传递日期的天气。但我们离题了。
最后,我构建了我的测试类来检查每次调用 setSelectedBean(...)时,VGObserver 更新(...)被触发。我使用Easy Mock来模拟VGObserver接口:
public class AppointmentBeanObservableTest {
private AppointmentFormBean form;
private VGObserver mockObserver;
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Before
public void setUp() {
form = new AppointmentFormBean();
mockObserver = createMock("observer", VGObserver.class);
form.registerVGObserver(mockObserver);
}
@After
public void tearDown() {
verify(mockObserver);
}
@Test
public void testIocForVGObserved() throws ParseException {
Date aDate = sdf.parse("2015-09-15");
form.setSelectedDate(aDate);
mockObserver.update(aDate);
EasyMock.expectLastCall().anyTimes();
replay(mockObserver);
}
}
不幸的是,这个测试在皇室上失败了,我找不到任何东西可以修补它。下面是github上的输出和项目链接:
Time elapsed: 0.091 sec <<< FAILURE!
java.lang.AssertionError: Expectation failure on verify:
observer.update(Tue Sep 15 00:00:00 EDT 2015): expected: at least 1, actual: 0
at org.easymock.internal.MocksControl.verify(MocksControl.java:241)
at org.easymock.EasyMock.verify(EasyMock.java:2100)
at com.vgorcinschi.rimmanew.model.AppointmentBeanObservableTest.tearDown(AppointmentBeanObservableTest.java:40)
Results :
Failed tests:
AppointmentBeanObservableTest.tearDown:40
Expectation failure on verify:
observer.update(Tue Sep 15 00:00:00 EDT 2015): expected: at least 1, actual: 0
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
https://github.com/vasigorc/rimmaproject
任何提示都表示赞赏。