java - Mockito when().thenReturn doesn't work -


i'm trying write tests spring controller , having problem. following code returns redirect:/welcome though have when(result.haserrors()).thenreturn(true); should return add. may i'm doing wrong. me solve this, please.

controller

@controller public class springcontroller {  @autowired private userservice userservice;  @autowired private correctvalidator correctvalidator;  @autowired private existvalidator existvalidator;  @autowired private unwrapper unwrapper;      @requestmapping(value = "/create", method = requestmethod.post)     public string create (wrapper wrapper,                       bindingresult result)         throws parseexception {         correctvalidator.validate(wrapper, result);         existvalidator.validate(wrapper, result);         if (result.haserrors()) {             return "add";         }         userservice.create(unwrapper.unwrap(wrapper));         return "redirect:/welcome";     } } 

test

@runwith(springjunit4classrunner.class) @webappconfiguration @contextconfiguration(locations={"file:src/main/webapp/web-inf/spring-servlet.xml"}) @testexecutionlisteners({ dependencyinjectiontestexecutionlistener.class}) public class controllertest {  @injectmocks private springcontroller controller;  @mock private wrapper wrapper;     @mock private bindingresult result;  @before public void setup() throws exception {     mockitoannotations.initmocks(this);     mockmvc = standalonesetup(controller)             .setsingleview(mockview)             .build(); }      @test     public void testcreatebad() throws exception {         when(result.haserrors()).thenreturn(true);          mockmvc.perform(post("/create", wrapper, result))                 .andexpect(status().isok())                 .andexpect(view().name("add"));     }  } 

the problem not using post() method correctly. see javadoc here.

in arguments pass

post("/create", wrapper, result) 

wrapper , result used url variables, not method arguments create method. cannot mock bindingresult way. it's extremely hard imo mock , not worth in long run. if should test command objects or won't valid.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -