Please provide a sample code snippet for controller Junit test without ZATs?

asked 2019-08-02 01:27:23 +0800

Pavan89 gravatar image Pavan89

I need to write Junit test case for controller witout ZATs

controller has the variables like the below @WireVariable private XXXXService xxxService;

private XXXXXObservable xxxxObservable;

protected Listbox businessCb;

protected Listbox typeCb;

how can i get the above variable for the junit test class.

Please help.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-08-02 10:21:50 +0800

cor3000 gravatar image cor3000
6032 2 7
ZK Team

updated 2019-08-02 10:41:36 +0800

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> {
        private MyService myService;
        public void submit() {
            System.out.println("submit to service: " + myService);

    public void testMyControllerSubmit() throws NoSuchFieldException, IllegalAccessException {
        MyService myService = new MyService();
        MyController myController = new MyController();

        Field field = MyController.class.getDeclaredField("myService");
        field.set(myController, myService);


Now that you have some code I hope it helps your efforts. Still I'd suggest ZATs to test your logic.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2019-08-02 01:27:23 +0800

Seen: 7 times

Last updated: Aug 02 '19

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More