-
FEATURED COMPONENTS
First time here? Check out the FAQ!
how to unit test viewModel methods ? can anybody share example code.
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;
@Command
public void clearName() {
this.name = null;
}
public void getName() {
return this.name;
}
/*setter*/
}
In your test you can then...
@Test
public void testClearName() {
MyViewModel vm = new MyViewModel();
vm.setName("initially filled");
//call @Command method directly
vm.clearName();
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
Asked: 2020-04-30 08:56:14 +0800
Seen: 4 times
Last updated: Apr 30 '20