java - The test returns true, while it should return false and vice versa -


continuation of thread how test spring boot using mockito. there problem.

well, method

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

after receiving username , checking in database should return false if username exists. however, test

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

returns true. have user username, , method should return false. however, not.

java.lang.assertionerror: response content  expected :false actual   :true 

i think understand syntax well

.andexpect(content().string("false")); 

i'm expecting string of false value. how tell service return?

you can mock service's return mockito's when.

@test public void textexistsusername() throws exception {     when(userservice.existsbyusername("jonki97")).thenreturn(true);     mockmvc             .perform(get("/checkuserdata/checkusername")             .param("username", "jonki97"))             .andexpect(status().isok())             .andexpect(content().string("false")); } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -