-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I need to write Junit test case for controller witout ZATs
controller has the variables like the below @WireVariable private XXXXService xxxService;
@WireVariable
private XXXXXObservable xxxxObservable;
@Wire
protected Listbox businessCb;
@Wire
protected Listbox typeCb;
how can i get the above variable for the junit test class.
Please help.
The quickest thing would be using reflection. By just setting the private fields on your test object - which is what ZK and any other wiring lib do anyway - you can inject your variable into your test object.
Other workarounds are providing setters. Or use utils like ReflectionTestUtils, which again do the same thing internally.
import org.junit.Test;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Div;
import java.lang.reflect.Field;
public class MyControllerTest {
public static class MyService {
}
public static class MyController extends SelectorComposer<Div> {
@WireVariable
private MyService myService;
@Listen("onClick=#submitButton")
public void submit() {
System.out.println("submit to service: " + myService);
}
}
@Test
public void testMyControllerSubmit() throws NoSuchFieldException, IllegalAccessException {
MyService myService = new MyService();
MyController myController = new MyController();
Field field = MyController.class.getDeclaredField("myService");
field.setAccessible(true);
field.set(myController, myService);
myController.submit();
}
}
Now that you have some code I hope it helps your efforts. Still I'd suggest ZATs to test your logic.
Asked: 2019-08-02 01:27:23 +0800
Seen: 8 times
Last updated: Aug 02 '19