Wednesday 15 April 2015

java - How to write Unit test with Mockito -


I have a simple controller, which I want to write a Mokto unit test. Here's the code:

  Personal End User Controller userCtl; Get public string (last model model) {return.userCtl.getLoginForm (model); }   

This is my test:

  @ mock private model model; Private DefaultControllerImpl Suite; @ Before public zero setup () {this.ctl = new DefaultControllerImpl (this.userCtl, this.authService, this.org service, this.riskSpaceService); This.ctl.setMessageSource (this.messageSource); } @Test Public Zero TestGate () {Final String View = this.sut.get (this.model); AssertThat (see) .isEqualTo (UserController.LOGIN_PATH); }   

However, this test always gives blank. How can I go about writing a proper unit test for this controller?

You can not say what is null But I believe that you do not have any mask, so you have to declare the following runners on your test class:

  @RunWith (MockitoJUnitRunner.class)   < P> However, I can not see the test. For example you are not asking to return anything  usrCtl  to getLoginForm (...) , so it By default  null  will return - maybe this is the The reason is that you are seeing  null   
  given (userCtl.getLoginForm (model))  

usrCtl copy To return, return the value you want: .willReturn (UserController.LOGIN_PATH);

For more information and examples, if you have not already done so, then you should take a look.

No comments:

Post a Comment