java - How to properly test a Spring Boot using Mockito -


i never did test , decided first. using spring boot , default layout follows: https://zapodaj.net/c8b65f1644e95.png.html file created on startup looks this

@runwith(springrunner.class) @springboottest public class restwebservicesapplicationtests {      @test     public void contextloads() {     }  } 

i not change here, because not need it. contrast, created new class

public class checkuserdatarestcontrollertest {      @mock     private userservice userservice;      @injectmocks     private checkuserdatarestcontroller checkuserdatarestcontroller;      private mockmvc mockmvc;      @before     public void setup() {         mockitoannotations.initmocks(this);          mockmvc = mockmvcbuilders.standalonesetup(checkuserdatarestcontroller).build();     }      @test     public void textexistsusername() throws exception {         mockmvc                 .perform(get("/checkusernameatregistering")                 .param("username", "jonki97"))                 .andexpect(status().isok());     } } 

to test controller method, should return status ok.

@getmapping("/checkusernameatregistering") public httpentity<boolean> checkusernameatregistering(@requestparam string username) {             return responseentity.ok().body(!userservice.existsbyusername(username)); } 

however, during test, throws me out

java.lang.assertionerror: status  expected :200 actual   :404  <click see difference>      @ org.springframework.test.util.assertionerrors.fail(assertionerrors.java:54)         ... 

this tested controller: https://pastebin.com/mww9ywih

i did not make special configurations because did not find out needed something.

the error 404 means mock not recognize endpoint test. end point should be:

 @test     public void textexistsusername() throws exception {         mockmvc                 .perform(get("/checkuserdata/checkusernameatregistering")                 .param("username", "jonki97"))                 .andexpect(status().isok());     } 

hope help.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -