Reputation: 3628
I have a form with buttons and group of radio buttons (SelectOneRadio). On click of the radio button, an ajax event will be fired and an listener in Java cntroller class will be executed for some calulcation.
**//Xhtml**
<p:selectOneRadio id="id_radio_group" value="#{bean.radioValue}">
<f:selectItem itemValue="a" itemLabel="a"></f:selectItem>
<f:selectItem itemValue="b" itemLabel="b"></f:selectItem>
<f:selectItem itemValue="c" itemLabel="c"></f:selectItem>
<f:selectItem itemValue="d" itemLabel="d"></f:selectItem>
<f:selectItem itemValue="e" itemLabel="e"></f:selectItem>
<f:ajax event="change" listener="#{controller.recordSelectedRadio}"></f:ajax>
</p:selectOneRadio>
**// Controller**
public void recordSelectedRadio(AjaxBehaviorEvent event) {
// Some logics to business specific
}
To write Junit for the recordSelectedRadio() in the controller? i tried by creating the new AjaxBehaviorEvent object. But the constructor of AjaxBehaviorEvent asks for 2 parameter UIComponent and Behaviour. I can get the UIComponent as SelectOneRadio. But i could not get the Behaviuor.
Any ideas on how the JUnit can be written for Ajax listeners of JSF?
Upvotes: 0
Views: 1270
Reputation: 2600
If you want to test the logic that will be called by the event handler, you can do that by delegating the business-logic. You would not implement your businesscode directly in the eventhandler recordSelectedRadio(...), instead you create a new Method myBusinessCode() and call that from recordSelectedRadio(...). The same Method can then be called from a TestCase.
Actually a better design would be to use BusinessObjects that contain your BusinessLogic to seperate the businesslogic from UI-Controllers. This is not only done to make the business logic testable, this will also separeate the GUI-Layer from the Business-Layer. In theory this seperation should even enable you to change the GUI-Framewework without changing the business-logic.
But I think what you really want to do is, front-end-testing. You cannot test the behavior of the radio-buttons etc. with JUnit. There is a tool called JSFUnit that might help you. A classical testing tool for UIs would be selenium.
Upvotes: 1
Reputation: 18153
Typically, one separates ones code into different layers for each responsibility and you should ask yourself whether you want to test that a certain action listener is called (which is more or less framework responsibility) or rather the business logic behind this action listener (which is business responsibility). Answering this question with the second answer, you should refactor your code such that the action listener calls a method which can be tested separately without any framework knowledge and only based on input data given by the JSF controller.
Upvotes: 1