How to Unit test ViewModel Annotation methods ?

asked 2020-04-30 08:56:14 +0800

wastemails gravatar image wastemails

how to unit test viewModel methods ? can anybody share example code.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-30 22:26:26 +0800

cor3000 gravatar image cor3000
5663 2 7
ZK Team

In the ideal case you implemented your ViewModel isolated from the UI as is the idea of MVVM. You should then be able to instantiate your ViewModel directly in your test case and then call the public methods directly.

Here a simple ViewModel:

public class MyViewModel {
    private String name;

    public void clearName() {
        this.name = null;

    public void getName() {
        return this.name;

In your test you can then...

public void testClearName() {
    MyViewModel vm = new MyViewModel();
    vm.setName("initially filled");

    //call @Command method directly
    Assert.assertNull("viewModel name should be empty", vm.getName());

As said this is the ideal case ... in an ideal MVVM scenario, as soon as you start mixing MVVM with MVC concepts these benefits go away and you'll have to start mocking out everything that's not provided during the runtime of your test case.

Or you could use ZATS which will emulate the client server environment by running an embedded jetty container starting your web application.

Here the related documentation including example code: https://www.zkoss.org/documentation/zats

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: 2020-04-30 08:56:14 +0800

Seen: 3 times

Last updated: Apr 30 '20

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