0

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

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

Pavan89 gravatar image Pavan89
1

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.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

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

cor3000 gravatar image cor3000
4606 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> {
        @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.

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

Follow
1 follower

RSS

Stats

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

Seen: 6 times

Last updated: Aug 02

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