Spring Mvc unit test using Mockito + MockMVC | Java Techie

Поделиться
HTML-код
  • Опубликовано: 9 сен 2024
  • This video explain you How to write unit test case for controller in spring mvc using MockMvc ?
    #JavaTechie #MockMVC #Junit #Controller
    GitHub:
    github.com/Jav...
    Blogs:
    javagyanmantra.wixsite.com/website
    Facebook:
    / 919464521471923
    Don't forgot to ( Like & Subscribe )

Комментарии • 133

  • @avijitsharma5050
    @avijitsharma5050 5 лет назад +17

    13:27 setup mockmvc
    16:57 result = mockMvc.perform(post("/url")) method
    20:10 result.getResponse().getContentAsString();
    20:41 Response.class

    • @Javatechie
      @Javatechie  5 лет назад

      Could you please elaborate what you want to convince ?

    • @avijitsharma5050
      @avijitsharma5050 5 лет назад +5

      Java Techie This is for my reference. If I need to see this video again, i can only see important points of video.

    • @Javatechie
      @Javatechie  5 лет назад +1

      @@avijitsharma5050 got it , cool

  • @VaibhavPawar12483
    @VaibhavPawar12483 5 лет назад +6

    dude this tutorial worked like a charm....keep the good work going....saved my day at work......thanks.....!!!!!!!

  • @neetajava7061
    @neetajava7061 2 года назад +3

    Excellent, All your video are relevant to the industry usage. Really helpful. Thank you.

  • @abhinavkumargurung3870
    @abhinavkumargurung3870 3 года назад +3

    Been watching a few of your videos. Really good content.Thanks for the effort.

  • @Deshbakth
    @Deshbakth Год назад +1

    Your knowledge is top, but please give clean description about what your doing and what is the each work it will reach bigners also thank you.

    • @Javatechie
      @Javatechie  Год назад

      Cool going forward i will take care of that

  • @Andrewtmcb
    @Andrewtmcb 5 лет назад +4

    Thank you for the inspiration at the beginning of the video :)

  • @rajnava944
    @rajnava944 Год назад +1

    Life saver. Best out there.

  • @perumalsamy5346
    @perumalsamy5346 4 года назад +2

    Very nice explanation. Thanks for the knowledge sharing.

  • @2RAJ21
    @2RAJ21 Месяц назад +1

    Thank you

  • @amargupta1728
    @amargupta1728 4 года назад +3

    Nice but can you explain in more detail What is Mokito, MockMVC, @before, MockMVC Builders etc...and when to use and how Boot will invoke this ...some at very beginner level? Also, why do we need to use MockMVC and can we do it normal jUnit and what are the advantages of using MockMvc? (Sorry I am new in testing and yeah so boring topic for any developers).

    • @Javatechie
      @Javatechie  4 года назад +1

      Ok I will create one Integration test video there I will explain all required annotation.

  • @suneelalavala9777
    @suneelalavala9777 5 лет назад +2

    Thank you. This video more help to me

  • @rakeshmallick6405
    @rakeshmallick6405 5 лет назад +1

    hi ur explaination is real good

  • @electrify9447
    @electrify9447 3 года назад +1

    Thank you very much for this video

  • @niyateejain4451
    @niyateejain4451 4 года назад +2

    I am getting status expected but was getting error. I have updated the content-type in the header to : application/json. Please suggest if I am missing something.
    My exact code is :
    String payload =""; //some json payload
    MvcResult result = mockMvc.perform(post("/abc/xyz").header("Content-Type","application/json")
    .contentType(MediaType.APPLICATION_JSON).content(payload)
    .accept(MediaType.APPLICATION_JSON))
    .isExpect(status().isOk()).andReturn();

    • @Javatechie
      @Javatechie  4 года назад

      Is this post API you are trying , u believe 415 means bad request something going wrong in your request
      Do one thing
      In payload , try to pass your actual object after converting it to string
      Use new ObjectMapper().writeValueAsString(payload);

    • @niyateejain4451
      @niyateejain4451 4 года назад +1

      @@Javatechie Yes, This is a post API I am trying.
      Since the payload is a string in itself so using new ObjectMapper().writeValueAsString(payload); i.e converting it into again into a string does not suffice

    • @Javatechie
      @Javatechie  4 года назад

      Can you show me your controller class

    • @niyateejain4451
      @niyateejain4451 4 года назад

      @@Javatechie How to since we work on remote desktops.can share via image but unable to upload in here

  • @kappaj01
    @kappaj01 4 года назад +5

    Use @Data - lot easier than adding @Getter and and and

  • @sahilsoni558
    @sahilsoni558 3 года назад +1

    Bhai you are God ❤️

  • @mobinshah1
    @mobinshah1 3 года назад +1

    I like YOUR COMMENTS ON YOUR DESKTOP SCREEN... :)

  • @srikanthkusa9615
    @srikanthkusa9615 2 года назад +1

    You are a best tutor Basant. Awesome. Could you please make a video on ReactJS?

    • @Javatechie
      @Javatechie  2 года назад

      I will start this series soon 🙂

  • @madasuyayelukumari5233
    @madasuyayelukumari5233 Год назад

    @Java Techie , I had given the return type as string in controller class for post method
    What i should i do for that

  • @nizamshaiktippu6389
    @nizamshaiktippu6389 3 года назад +1

    Good Tutorial

  • @daniellaerachannel
    @daniellaerachannel 5 лет назад +7

    good tutorials, try to improve the microphone quality maybe, it'd be great

    • @Javatechie
      @Javatechie  5 лет назад +1

      I Really appreciate your support and interest definitely going forward I will improve it

  • @mugeeshhusain4498
    @mugeeshhusain4498 2 года назад +1

    very good video

  • @hema2564
    @hema2564 9 месяцев назад +1

    Will junit testing hit database?or we should mock that..

    • @Javatechie
      @Javatechie  9 месяцев назад

      If you won't mock it will hit the db

  • @bimalsahu1251
    @bimalsahu1251 5 лет назад +2

    hi sir I have a doubt on Junit Test cases writing on Only For a controller or service Layer ?

    • @Javatechie
      @Javatechie  5 лет назад

      Hi Bimal,
      Normally we are writing test case for both controller and service layer .
      For controller we need to use mockMVC where for service or Dao we need to use Mockito
      Already I have uploaded video for both layer please have a look

  • @praveenkumart4106
    @praveenkumart4106 4 года назад +3

    JUnit is excluded in latest spring web starter package. Need to use JUnit Jupiter so can you create a video with that.

    • @Javatechie
      @Javatechie  4 года назад

      Spring boot 2.x onwords it used Jupiter
      You no need to add it manually

  • @deepika1040
    @deepika1040 Год назад

    How to pass Boolean value in setter ???? Like if we writter in setter as Boolean success .....then in junit case ....how to pass Boolean value in ...setter ????

  • @quicknews213
    @quicknews213 3 года назад +1

    nice work bro keep it up

  • @dineshmetkari1574
    @dineshmetkari1574 4 года назад

    which short cut you are using for indendation...I mean after select+All which key you are using for auto-idendation

  • @gowtham7819
    @gowtham7819 2 года назад +1

    Thanks bro

  • @sambhajikale7338
    @sambhajikale7338 5 лет назад +1

    I am getting 400 in my project.writevalueasString not generating proper JSON .I am coping same JSON and hitting a APi through postman but not working.

    • @Wind-on-myFace
      @Wind-on-myFace 3 года назад

      @RunWith(SpringJUnit4ClassRunner.class)
      @WebAppConfiguration
      Try including this

  • @vijayr6540
    @vijayr6540 4 года назад +2

    Thankyou !

  • @Rakeshkumar-fn1sv
    @Rakeshkumar-fn1sv 6 лет назад +3

    Hi sir if you have an application that is based on real time project and if you used the junit test case for that project then please add the link , because i don't know how to check the list of data that is coming form the Database , i want to test of that data , so please add the link sir or even you can upload in the face book group .
    Please sir , i have need of this .

    • @johnjo6414
      @johnjo6414 6 лет назад

      You need to mock your dependencies like database with Mockito library

  • @shraddhashree8995
    @shraddhashree8995 3 года назад

    How to write test case for controller method having return type ResponseEntity . Plz help..

  • @amanabhishek8998
    @amanabhishek8998 3 года назад +1

    Please make video on test service and repository class

  • @rabindrasahu3610
    @rabindrasahu3610 6 лет назад +1

    Basanta bhai your video is so nice but the problem is visibility,plz go through it.

    • @Javatechie
      @Javatechie  6 лет назад +1

      Hi Rabindra, Thanks for your update it depends on your internet connectivity so please try to connect with good internet it will run on HD mode

  • @user-gc8ev1tm1t
    @user-gc8ev1tm1t 7 месяцев назад

    @javatechonline. i want to mock service layer also so that it doesn't hit actual db layer

  • @taraprasadsarangi986
    @taraprasadsarangi986 4 года назад +1

    want to know how to use Mockito.when() , thenReturn() and Mockito.verify() in this example.i dont want to affect the db while executing Database

    • @Javatechie
      @Javatechie  4 года назад +1

      Already I created another mockito tutorial please check that

    • @taraprasadsarangi986
      @taraprasadsarangi986 4 года назад

      @@Javatechie ok I'll look into it

  • @nehapalnullRA
    @nehapalnullRA 3 года назад +1

    under @autowired, what is "WebApplicationContext Context"? is that a class and its object that you have created? or it has to be mandatorily written?

    • @Javatechie
      @Javatechie  3 года назад +1

      Yes you must need to autowire this class to kick start test web context

    • @nehapalnullRA
      @nehapalnullRA 3 года назад +1

      @@Javatechie for statements like squery=squery.replace("", pid);
      System.out.println(squery);
      return jdbcTemplate.query(squery, (rs, rowNum) -> new StudyUID(
      rs.getInt("PERSON_ID"),
      how to write test case for this scenario?

    • @Javatechie
      @Javatechie  3 года назад

      Please checkout below tutorial and mock your dao class
      ruclips.net/video/kXhYu939_5s/видео.html

    • @nehapalnullRA
      @nehapalnullRA 3 года назад

      @@Javatechie i am getting error :com.fasterxml.jackson.databind.exc.MismatchedInputException
      Cannot deserialize my response class. what could be the possible solution?

  • @mdmuntazim4384
    @mdmuntazim4384 2 месяца назад +1

    Does it hit database in every call???

    • @Javatechie
      @Javatechie  2 месяца назад

      No it won't that's why we mock the Dao

    • @mdmuntazim4384
      @mdmuntazim4384 2 месяца назад

      @@Javatechiewould the junit work if we create controlTest.java and execute there insteam of mainTest.class??

  • @hussainbasha8106
    @hussainbasha8106 6 лет назад

    nice explanation ...bro

  • @vijik2988
    @vijik2988 2 года назад +1

    Please I request to do Mockito testing for Repository layer i am not getting any good article
    I am stuck
    1 question i,e when we are testing controller layer we should mock repository layer also na sir but u have not mocked
    Thank You.

    • @Javatechie
      @Javatechie  2 года назад

      No we shouldn't mock repository if you are writing test case for controller

    • @vijik2988
      @vijik2988 2 года назад +1

      @@Javatechie
      Y ?
      Please make mockito testing for repository layer

    • @Javatechie
      @Javatechie  2 года назад

      Okay i will do that using test container

  • @sushantpandey1949
    @sushantpandey1949 3 года назад

    Can we mock the DAO in the flow above?

  • @seetaramaraotaduri90
    @seetaramaraotaduri90 4 года назад

    Thanks for this wonderful video

    • @seetaramaraotaduri90
      @seetaramaraotaduri90 4 года назад

      Can you make video on sending objects into rabbitmq and if it fails placed them on failed queues using spring boot retry

    • @Javatechie
      @Javatechie  4 года назад +1

      Yup i can , as I noticed maximum viewers suggested for active Mq let me give a try

    • @seetaramaraotaduri90
      @seetaramaraotaduri90 4 года назад

      @@Javatechie most of the videos on RUclips is not showing how to work on fail queues .. your example can have list of objects like 100 records from CSV file that are sent to the queue and store them in database while reading back from queue and due to network issue or any other issue the messages should go to the failed queues and later read back from it. . Your examples are always unique .

    • @Javatechie
      @Javatechie  4 года назад +1

      @@seetaramaraotaduri90 thanks for giving me wonderful scenario .
      Definitely I will do this .but before that I would like to introduce small content on retry mechanism else it confuse user to understand queue with retry .
      Make sense ?

    • @seetaramaraotaduri90
      @seetaramaraotaduri90 4 года назад

      @@Javatechie thank for your reply .. yes I agree start with a small example and later can elaborate on this .

  • @mugeeshhusain4498
    @mugeeshhusain4498 2 года назад

    can you tell me when to mock repository or when to mock service , in this video you are mocking service ?

  • @nehapalnullRA
    @nehapalnullRA 3 года назад +1

    If we do not have response class, what should we use instead of it?

    • @Javatechie
      @Javatechie  3 года назад +1

      You need to design this based on your return type

    • @nehapalnullRA
      @nehapalnullRA 3 года назад +1

      @@Javatechie have you used JUnit4?

    • @Javatechie
      @Javatechie  3 года назад +1

      Yes I used

  • @sushantbhardwaj3080
    @sushantbhardwaj3080 4 года назад +1

    thanks

  • @shrutipatil4555
    @shrutipatil4555 4 года назад +1

    Hi Basant. Will you please use microphones, your voice is too low in all your videos.

    • @Javatechie
      @Javatechie  4 года назад

      I agreed Shruti , these are old videos but in recent videos voice and video clarity both are good .

  • @pawanBilaspur
    @pawanBilaspur 3 года назад +1

    Well this is not unit testing
    You are creating an client for rest apis
    If your are writing test cases for controller layer then service layer should be mocked

    • @Javatechie
      @Javatechie  3 года назад

      This is integration test Manish

    • @pawanBilaspur
      @pawanBilaspur 3 года назад

      @@Javatechie headline to Junit laga rakha hai...

  • @giridharsai6562
    @giridharsai6562 Год назад

    Can u please upload the exact code in git ...the link u posted for this is quite different

  • @ankitrawat8465
    @ankitrawat8465 4 года назад

    Can you see the data in the database that was created by junit test first case

  • @wantedlepota9960
    @wantedlepota9960 Месяц назад +1

    setup or creating new object was not supposed to be part of this tutorials. that could have been created already

    • @Javatechie
      @Javatechie  Месяц назад

      Yes agree but it's demo need to cover from scratch

    • @wantedlepota9960
      @wantedlepota9960 Месяц назад +1

      @@Javatechie someone who wants to learn mockito, would already know those basics

    • @Javatechie
      @Javatechie  Месяц назад +1

      I completely understand your concern buddy what I am trying to convince that beginners do watch my videos who started learning from basics so if anyone already advanced then he/she have option to skip and watch the videos from where they really need . Hope this makes sense

    • @wantedlepota9960
      @wantedlepota9960 Месяц назад +1

      @@Javatechie fair enough. thank you

  • @nehajaiswal3288
    @nehajaiswal3288 5 лет назад +1

    Hi..m getting error like content type is missing...please suggest

    • @Javatechie
      @Javatechie  5 лет назад +1

      Did you mentioned media type as application/json in header

    • @nehajaiswal3288
      @nehajaiswal3288 5 лет назад +1

      @@Javatechie I mentioned that one

    • @Javatechie
      @Javatechie  5 лет назад

      Please follow same what I did .still if u r facing same issue then please share ur code in javatechie4u@gmail.com

    • @nehajaiswal3288
      @nehajaiswal3288 5 лет назад +1

      com.fasterxml.jackson.databind.exc.MismatchedInputException
      Cannot deserialize my response class

    • @nehajaiswal3288
      @nehajaiswal3288 5 лет назад +2

      @@Javatechie i have sent the code...Please check once

  • @sumandas-cm3xu
    @sumandas-cm3xu 5 лет назад

    you tell wrong input statement for recovery this error status code 415 but i cant understand. ..please help me

    • @Javatechie
      @Javatechie  5 лет назад

      Could you please mention your exact issue?
      R u talking about which method ?
      If you are getting 415 then it's media type issue . please cross verify once
      Did you specified media type as application/json in headers and in controller URL mapping .?

    • @sumandas-cm3xu
      @sumandas-cm3xu 5 лет назад +2

      @@Javatechie i get this...changes content to contentType fix this issue

  • @basmaradadi9155
    @basmaradadi9155 4 года назад

    what you dont have a service laye in your project ?

    • @Javatechie
      @Javatechie  4 года назад +1

      Hi Basma , it's good practice to write service layer in your application .
      But in my max video am not writing service layer to avoid time duration

    • @basmaradadi9155
      @basmaradadi9155 4 года назад

      @@Javatechie u dont have plz an example for unit test for controller layer where u used (u moked ) the service layer in it ?

  • @100bands
    @100bands 3 года назад

    Testing starts at 11:25

  • @pawandekate7052
    @pawandekate7052 3 года назад +1

    Java Techie, while running test cases i get fallowing exception
    java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.reflection.ReflectionCache

    • @Javatechie
      @Javatechie  3 года назад

      Please reimport your pom and error says dependency is not available at runtime

  • @sonalsinha2381
    @sonalsinha2381 3 года назад +1

    Its giving me the NullPointerException

    • @Javatechie
      @Javatechie  3 года назад

      Please checkout my source code

  • @beastbaby7946
    @beastbaby7946 2 года назад +1

    very low voice pls fix

    • @Javatechie
      @Javatechie  2 года назад

      Buddy these are my old videos please coperate

  • @rachaitachakravarty7893
    @rachaitachakravarty7893 4 года назад

    Please share the spring annotations document.

  • @alexeystaroverov4804
    @alexeystaroverov4804 5 лет назад +1

    Inject context into your code is a very bad practice

    • @Javatechie
      @Javatechie  5 лет назад

      Sorry didn't get you , you mean injecting WebApplicationContext in junit

  • @kimangel1325
    @kimangel1325 4 года назад +1

    This is not unit test
    This is integration test
    You need to mock all your dependencies in this case respository

  • @riddhisworldofart681
    @riddhisworldofart681 4 года назад

    can you pls share the complete src

    • @Javatechie
      @Javatechie  4 года назад

      In video description I mentioned my GitHub link so you download my code

  • @DudeMcSky
    @DudeMcSky 3 года назад

    Light Mode? How dare you...

  • @ekadet7882
    @ekadet7882 4 года назад +1

    boring