Spring Mvc unit test using Mockito + MockMVC | Java Techie

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

ความคิดเห็น • 133

  • @VaibhavPawar12483
    @VaibhavPawar12483 5 ปีที่แล้ว +6

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

  • @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

  • @neetajava7061
    @neetajava7061 3 ปีที่แล้ว +3

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

  • @abhinavkumargurung3870
    @abhinavkumargurung3870 4 ปีที่แล้ว +3

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

  • @perumalsamy5346
    @perumalsamy5346 4 ปีที่แล้ว +2

    Very nice explanation. Thanks for the knowledge sharing.

  • @rajnava944
    @rajnava944 ปีที่แล้ว +1

    Life saver. Best out there.

  • @Andrewtmcb
    @Andrewtmcb 5 ปีที่แล้ว +4

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

  • @quicknews213
    @quicknews213 4 ปีที่แล้ว +1

    nice work bro keep it up

  • @sahilsoni558
    @sahilsoni558 3 ปีที่แล้ว +1

    Bhai you are God ❤️

  • @rakeshmallick6405
    @rakeshmallick6405 6 ปีที่แล้ว +1

    hi ur explaination is real good

  • @Deshbakth
    @Deshbakth 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      Cool going forward i will take care of that

  • @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 🙂

  • @nizamshaiktippu6389
    @nizamshaiktippu6389 3 ปีที่แล้ว +1

    Good Tutorial

  • @madasuyayelukumari5233
    @madasuyayelukumari5233 2 ปีที่แล้ว

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

  • @electrify9447
    @electrify9447 3 ปีที่แล้ว +1

    Thank you very much for this video

  • @suneelalavala9777
    @suneelalavala9777 5 ปีที่แล้ว +2

    Thank you. This video more help to me

  • @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

  • @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

  • @mugeeshhusain4498
    @mugeeshhusain4498 3 ปีที่แล้ว +1

    very good video

  • @mobinshah1
    @mobinshah1 4 ปีที่แล้ว +1

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

  • @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.

  • @hussainbasha8106
    @hussainbasha8106 6 ปีที่แล้ว

    nice explanation ...bro

  • @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

  • @bimalsahu1251
    @bimalsahu1251 6 ปีที่แล้ว +2

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

    • @Javatechie
      @Javatechie  6 ปีที่แล้ว

      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

  • @hema2564
    @hema2564 ปีที่แล้ว +1

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

    • @Javatechie
      @Javatechie  ปีที่แล้ว

      If you won't mock it will hit the db

  • @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 TH-cam 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 .

  • @taraprasadsarangi986
    @taraprasadsarangi986 5 ปีที่แล้ว +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  5 ปีที่แล้ว +1

      Already I created another mockito tutorial please check that

    • @taraprasadsarangi986
      @taraprasadsarangi986 5 ปีที่แล้ว

      @@Javatechie ok I'll look into it

  • @2RAJ21
    @2RAJ21 5 หลายเดือนก่อน +1

    Thank you

  • @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 4 ปีที่แล้ว

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

  • @shraddhashree8995
    @shraddhashree8995 4 ปีที่แล้ว

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

  • @dineshmetkari1574
    @dineshmetkari1574 4 ปีที่แล้ว

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

  • @GauravGarg-q4t
    @GauravGarg-q4t 11 หลายเดือนก่อน

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

  • @sushantpandey1949
    @sushantpandey1949 4 ปีที่แล้ว

    Can we mock the DAO in the flow above?

  • @amanabhishek8998
    @amanabhishek8998 3 ปีที่แล้ว +1

    Please make video on test service and repository class

  • @ankitrawat8465
    @ankitrawat8465 4 ปีที่แล้ว

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

  • @mdmuntazim4384
    @mdmuntazim4384 6 หลายเดือนก่อน +1

    Does it hit database in every call???

    • @Javatechie
      @Javatechie  6 หลายเดือนก่อน

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

    • @mdmuntazim4384
      @mdmuntazim4384 6 หลายเดือนก่อน

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

  • @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

  • @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 ????

  • @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

  • @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
      th-cam.com/video/kXhYu939_5s/w-d-xo.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?

  • @vijayr6540
    @vijayr6540 5 ปีที่แล้ว +2

    Thankyou !

  • @kappaj01
    @kappaj01 4 ปีที่แล้ว +5

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

  • @100bands
    @100bands 4 ปีที่แล้ว

    Testing starts at 11:25

  • @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

  • @giridharsai6562
    @giridharsai6562 2 ปีที่แล้ว

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

  • @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

  • @basmaradadi9155
    @basmaradadi9155 5 ปีที่แล้ว

    what you dont have a service laye in your project ?

    • @Javatechie
      @Javatechie  5 ปีที่แล้ว +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 5 ปีที่แล้ว

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

  • @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

  • @mugeeshhusain4498
    @mugeeshhusain4498 3 ปีที่แล้ว

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

  • @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 .

  • @sushantbhardwaj3080
    @sushantbhardwaj3080 4 ปีที่แล้ว +1

    thanks

  • @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

  • @rachaitachakravarty7893
    @rachaitachakravarty7893 5 ปีที่แล้ว

    Please share the spring annotations document.

  • @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

  • @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...

  • @sonalsinha2381
    @sonalsinha2381 3 ปีที่แล้ว +1

    Its giving me the NullPointerException

    • @Javatechie
      @Javatechie  3 ปีที่แล้ว

      Please checkout my source code

  • @wantedlepota9960
    @wantedlepota9960 5 หลายเดือนก่อน +1

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

    • @Javatechie
      @Javatechie  5 หลายเดือนก่อน

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

    • @wantedlepota9960
      @wantedlepota9960 5 หลายเดือนก่อน +1

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

    • @Javatechie
      @Javatechie  5 หลายเดือนก่อน +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 5 หลายเดือนก่อน +1

      @@Javatechie fair enough. thank you

  • @kimangel1325
    @kimangel1325 4 ปีที่แล้ว +1

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

  • @beastbaby7946
    @beastbaby7946 2 ปีที่แล้ว +1

    very low voice pls fix

    • @Javatechie
      @Javatechie  2 ปีที่แล้ว

      Buddy these are my old videos please coperate

  • @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

  • @DudeMcSky
    @DudeMcSky 4 ปีที่แล้ว

    Light Mode? How dare you...

  • @ekadet7882
    @ekadet7882 4 ปีที่แล้ว +1

    boring

  • @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