Writing JUnit Tests for RestController using MockMvc and Mockito | Tech Primers

Поделиться
HTML-код
  • Опубликовано: 9 сен 2024

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

  • @SushilYadav7
    @SushilYadav7 4 года назад +4

    When I search any topic on youtube and I see Tech Primers' video on it, I click it immediately.

  • @wallysonruan
    @wallysonruan 2 года назад +2

    Thank you! I've gone through a lot of Udemy courses and none of them helped me so much as you did. Keep up the good work!

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

    Thank you for the wonderful presentation. I believe you are a life saver for many victims who struggle to understand Junit test cases.

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

    I have said this before as well. You don't drag things and finish the course in quick time and make sure everything is covered in detail. Thank you

  • @AmitSingh-vb6oj
    @AmitSingh-vb6oj 5 лет назад +3

    To add - if your controller function is using some service class (which is most likely) then one needs to add
    @Mock
    ControllerService controllerService
    Before invoking test function

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

    nice one. Its very crisp and to the point. Thanks for the nice video. Keep rocking with more videos like this!!

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

    A perfect, precise, and clean tutorial.

  • @altjedan7067
    @altjedan7067 5 лет назад +3

    Thank you a TON.
    This was literally what i was looking for.
    Clean, detailed and precise tutorial.
    Respect.

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

    This is exactly what I've been looking for! Thanks for making this!

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

    Great video mate! Appreciate the time to share it with us!

  • @123rubyg
    @123rubyg 3 года назад +1

    Solid and crisp explanations.

  • @miguelrico4338
    @miguelrico4338 6 лет назад +3

    You are one of the best teachers on youtube! Love the content it's been really helpful!

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

      Thanks Miguel. Glad that was useful

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

    Thanks a lot, it is very helpful for me to understand clearly about the testing from Mock MVC.🙏

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

    Awesome Tutorial.

  • @yashobantamajhi8626
    @yashobantamajhi8626 6 лет назад +3

    Nice video for mockito.
    How to mock a end point of a controller which involves a interface.

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

    Very good Explanation of basic concepts great for beginners to start with.

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

    Very intersting...👍👍👍👍👍

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

    Can you please cover these tests with Junit5? I think lot of these things are changed.

  • @MrLangam
    @MrLangam 6 лет назад +3

    Wow, even though you have an odd accent, you seem to know what you are talking about unlike other indian youtubers out there who just mumbles stuff. Thanks!

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

    Found Useful in 2020!

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

    Unique and awesome video. Please can you create a playlist on JUNIT testing with some complex REST Example.

  • @abhilashsulibela1414
    @abhilashsulibela1414 6 лет назад +2

    Love the content. Simply brilliant

  • @motolola
    @motolola 5 лет назад +3

    Very nice Tutorial, was wondering about Testing Crud operations the same way.

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

    While running test case in eclipse getting socket connect timeout error how to resolve it?

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

    Nice and simple introduction.

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

    Thanks for the video mate

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

    Very nicely explained.

  • @e.ch.vidyasagarkorada7341
    @e.ch.vidyasagarkorada7341 3 года назад

    Really helps lot #KnowledgeSharing thank you @TechPrimers

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

    child example, bro in controllers we have service dependency and in service will be having respository dependency in your example those things were skipped, how to do integration test through mock mvc?

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

    Very good and detailed explaination

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

    Really Awesome video. Learned a lot.
    Good Job man.

  • @muks
    @muks 6 лет назад +2

    Can you add a test for writing a JUnit test case for ResponseEntity also? It is very useful as real world uses response entity to return response.
    Much appreciated.
    Thanks

  • @gaurav-javatechgeek935
    @gaurav-javatechgeek935 4 года назад

    thankyou , it was really help full, could you please provide some more videos for this

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

    Hey, it's easy to do it with spring boot which already comes with tomcat builtin, but how do you unit test API in spring mvc ? can you make video on that ?

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

      Unit tests are at the class level. So essentially testing an API mostly will have mocked classes. If you have services or DAOs, then you will need to have unit tests at each level. So unit testing doesn't fall under API level umbrella

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

    Getting NullPointerException, why> and how to solve pls explain

  • @andrzejokon1517
    @andrzejokon1517 6 лет назад +2

    Good tutorial. Thank you !

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

    Guys the test cases described here are called integration test cases or unit?

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

    In my STS just gives "NullPointerException". I have the same code but the result is the same

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

      you probably have dependencies you are not satisfying. You need to mock every other service. This was from four months ago but let me know if you still have some confusion and we can discuss.

    • @zishan-khan
      @zishan-khan 4 года назад

      @@Andrewtmcb Hi Andy I am having the problem. I did everything as mentioned in the video but I am getting Null Pointer Exception

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

    Failed to load Applicationcontext why I’m getting this error?

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

    Hi , why you didnt have w service layer in ur project

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

    Awesome but how to write unit test cases for a rest api which system depended to generate response.mine is a rest end point which only runs in suse linux environment but we do development on windows platform

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

      you will have to mock those system dependent responses or you need a Continuous Integration server which is based on suse linux env.

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

      @@TechPrimers thanks a lot.you are awesome.keep up video making

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

    What about if the HelloResource are using a real service? How do you mock that?

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

    Its throwing NullPointerException on this mockmvc = MockMvcBuilders.standaloneSetup(helloController).build();
    mockmvc is coming null.

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

    Thanks a lot sirji

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

    I get stuck while running the test for first time with this error "java.lang.AssertionError: Status expected: but was:
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)". Any clue on what could be wrong - code is exactly the same as yours ?

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

      This error occurs when either your rest url is wrong or when your server is not up or if the RestController is not identified by SpringBoot during startup

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

    how to test the Repository class methods. please post a tutorial.

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

    Very very helpful tutorial video..

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

    The video was really helpful. thankyou. i am working on writing junit test case for controller where i have to test for not a single entity but a list as a whole using jdbc template. how do i do that? i tried using object mapper, but i see errors while using it.

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

    Hi, I am facing an issue when running my test with @Valid and Errors handler on controller. It gives illegal state exception although i the Errors interface is called after the request body.

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

    Could you make a new video using junit 5? I get stuck at initiating the test class

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

    Thank you. very Helpful

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

    im geting the error "the method get(String) is undefined for the type HelloResourceTest" in line 36. same for "status()"

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

      import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
      import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
      import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

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

    This test applies for all controller,service and repository layers?

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

    thank you so much

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

    Thanks for this video.

  • @brianocorner5604
    @brianocorner5604 6 лет назад +2

    Hi Buddy, It is an awesome demo. But I have a doubt, In this video you are testing every URL with the data given in your rest method, but we can not provide hard code value in our controller. So, how can we provide mock data and test with that value? A quick response will be greatly appreciated. Thank you in advance.

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

      Yes Brian. In that case you can have the JSON value separated out in a file and compare the content

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

      @@TechPrimers Can you give an example how we can compare a json file stored int test/resources to the json response received by the api

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

    Hey can you please write some junit for post methods as well

  • @JohnWick-zc5li
    @JohnWick-zc5li 7 лет назад +1

    thanks tech primers

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

    Perfect🔥

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

    Hi brother,
    Could you share any content for writing positive and negative mockito test cases scenario for two tables which is related as many to one mapping.
    E.g., like many student belong to one department.
    Thanks.

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

    Good explaination

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

    Testing with Profiles?

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

    how do we load properties file

  • @pramodar6437
    @pramodar6437 6 лет назад +2

    Cool. Thnks.

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

    This is great - but how do you do this if you're working on an existing project that doesn't use Boot & won't be using it in the foreseeable future?

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

      If you are using Spring REST, then you can use it the same way

  • @dileepalla6769
    @dileepalla6769 7 лет назад +1

    THQ tech primers

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

    Awesome

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

    awesome

  • @KK-tz5fr
    @KK-tz5fr 5 лет назад +1

    thanks a lot

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

    Hello Sir, can you upload a video teaching how to write the JUNIT test cases for the methods(which has 2 or 3 business logics ) returning VOID?

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

    greatt videoo thank youu so muchh

  • @1sbollap
    @1sbollap 6 лет назад

    can you use MockMvc to test an application which is not built using Spring

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

    Is it possible to write test cases for SOAP endpoints?
    if Yes, can you make a sample video for that as well?
    Thank you

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

      Sure will do mate

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

      @@TechPrimers Thank you

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

      @@TechPrimers Can you explain this to me please. If you had to create the object with the builder, then what did injectmocks do ?Same helloresource you injected as well as created with builder. Can you please help me understand? Thank you very much.

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

    April 2021:
    With latest spring version: 2.4.4, below code works.
    @ExtendWith(SpringExtension.class)
    @WebMvcTest (HelloResource.class)
    class RestControllerTestApplicationTests {
    @Autowired
    private MockMvc mockMvc;
    @Test
    void contextLoads() {
    }
    @Test
    public void test() throws Exception {
    mockMvc.perform(get("/hello"))
    .andExpect(status().isOk())
    .andExpect(content().string("hello World"));
    }
    @Test
    public void testJson() throws Exception {
    MvcResult mvc = mockMvc.perform( get ("/hello/json").accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andExpect( jsonPath("$.title").value("Greetings") )
    .andExpect(jsonPath("$.value").value("Hello World") ).andReturn();
    Map objectMap = JsonPath.read(mvc.getResponse().getContentAsString(), "$");
    assertEquals(2, objectMap.keySet().size());
    }
    }

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

    AspectJ for AOP will be very awesome if you can do it. Thanks

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

    Brilliant bro :)

  • @JohnWick-zc5li
    @JohnWick-zc5li 7 лет назад +1

    how write test a contoller that calls a service method

    • @ainuddinb
      @ainuddinb 7 лет назад

      You can autowire the service in test class.

    • @TechPrimers
      @TechPrimers  7 лет назад

      If you are aiming to do only JUnit, then you can annotate the service with @Mock in the test and mock the service calls using Mockito.when() and verify them using Mockito.verify()

    • @TechPrimers
      @TechPrimers  7 лет назад

      Hi John, I have made a video on mocking the Service methods. Check it here: ruclips.net/video/Ijj5RLSAcYw/видео.html

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

    sir please, extend this video with jpa as well

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

    How about for nested json?

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

      you can try this: stackoverflow.com/questions/15371022/springmvc-mockmvc-jsonpath-compare-list-of-strings

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

    please upload a video on JUnit Tests for Multipart file upload using MockMvc......like for
    @PostMapping("/signSetupWithImage/{id}")
    public String initSetUpConfiguration(@RequestParam("file") MultipartFile file, @PathVariable Long id)

  • @ranjeetsahani9207
    @ranjeetsahani9207 6 лет назад +3

    Good content

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

      +ranjeet sahani thanks Ranjeet

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

    Way too many ads :(

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

    Why is there a background music? That's annoying af