Mock Service in RestController and Writing JUnits for POST with MockMvc | Tech Primers

  Рет қаралды 39,693

Tech Primers

Tech Primers

Күн бұрын

This video covers how to Mock Service in RestController along with testing POST with MockMVC
🔗Github Code for the tests: github.com/Tec...
📌 Related Playlist
================
🔗Spring Boot Primer - • Spring Boot Primer
🔗Spring Cloud Primer - • Spring Cloud Primer
🔗Spring Microservices Primer - • Spring Microservices P...
🔗Spring JPA Primer - • Spring JPA Primer
🔗Java 8 Streams - • Java 8 Streams
🔗Spring Security Primer - • Spring Security Primer
🔗Containers Primer - • Containers Primer
🔗Kubernetes Primer - • Kubernetes Primer
🔗AWS Primer - • AWS Primer
💥Join TechPrimers Slack Community: bit.ly/JoinTec...
💥Telegram: t.me/TechPrimers
💥TechPrimer HindSight (Blog): / techprimers
💥Website: techprimers.com
💥Slack Community: techprimers.sl...
💥Twitter: / techprimers
💥Facebook: TechPrimers
💥GitHub: github.com/Tec... or techprimers.gi...
🎬Video Editing: iMovie
🎼Background Music: Broke For Free - Day Bird
brokeforfree.b...
The Passion HiFi - What We Came To Do
---------------------------------------------------------------
🔥 Disclaimer/Policy:
The content/views/opinions posted here are solely mine and the code samples created by me are open sourced.
You are free to use the code samples in Github after forking and you can modify it for your own use.
All the videos posted here are copyrighted. You cannot re-distribute videos on this channel in other channels or platforms.
#Testing #Mockito #TechPrimers

Пікірлер: 46
@navalgupta9545
@navalgupta9545 6 жыл бұрын
Thanks for the simplest explanation of mocking rest services using Sprint Junit Runner.
@TechPrimers
@TechPrimers 6 жыл бұрын
thanks naval
@vamshikrishna8143
@vamshikrishna8143 6 жыл бұрын
power mock for static and private methods would be a great addition.
@abhilashsulibela1414
@abhilashsulibela1414 6 жыл бұрын
Awesome content. Please keep making more!
@navidgalt2244
@navidgalt2244 6 жыл бұрын
Very nice video! Can you show how to create tests for a POST method for REST services (passing headers and a body) with SpringMVC?
@shruthishru3553
@shruthishru3553 2 жыл бұрын
Very helpful, easy to understand, thanks 😊
@koushivlogs77
@koushivlogs77 5 жыл бұрын
Hi..excellent tutorial for beginners. Please create video on 100 percentage test coverage for a pojo annotated with lombok
@richardlanglois5183
@richardlanglois5183 6 жыл бұрын
Very useful presentation!
@lalitkulkarni6768
@lalitkulkarni6768 6 жыл бұрын
Not all post methods return back some json. Thus it becomes important to understand how to write a junit test in a scenario when we have a post method which inserts some data in the database by the route Rest controller --> Business --> Repository. This method doesn't return back any data. Can you please mention what should be the assertions or checks in this case? And what needs to be mocked in such cases.
@AshishGupta-rh3jf
@AshishGupta-rh3jf 6 жыл бұрын
I was struggling to write test cases for services. Thanks a lot for this video. Can you help to prepare for C# restful services testing.
@jasper5016
@jasper5016 3 жыл бұрын
Its good information but can you cover Mockito testing of CRUD operations? It would super helpful
@sureshdhulipudi600
@sureshdhulipudi600 5 жыл бұрын
How to mock Rest Template, i.e in service layer we r consuming rest end point
@jsloth0303
@jsloth0303 4 жыл бұрын
awesome video !
@tw5529
@tw5529 3 жыл бұрын
Awesome 👍😊 . If there will be a Controller Interface and all the methods iside Interface will be annotated with @GetMapping or @PostMapping . Controller class implements that Controller Interface.Then what exactly do we need to make change here??
@JohnWick-zc5li
@JohnWick-zc5li 7 жыл бұрын
thanks for vidoes -- how mock a final class, interface, & static method..
@TechPrimers
@TechPrimers 7 жыл бұрын
+John Wick you can use PowerMock to do that. I will see if I can do a video on that
@javatar4809
@javatar4809 7 жыл бұрын
Please make videos on testing RestController in spock framework...
@TechPrimers
@TechPrimers 7 жыл бұрын
Sure Javatar, Will do
@VivekSharma-qs6qp
@VivekSharma-qs6qp 5 жыл бұрын
Hi dude. At 2:48 You show that how we can use thenReturn() method to provide the response of the helloService.hello() method. In my case, I have a custom object that is being returned instead of a plain string. How to specify it in the thenReturn method?
@rossayer7729
@rossayer7729 3 жыл бұрын
The music listed in the description is not what I'm hearing in the video. Where do we find the song playing in the video? Edit: Found it in another Tech Primers video: Lowrider, Luvly - Joakim Karud
@ainuddinb
@ainuddinb 7 жыл бұрын
Very useful :) Thank you.
@TechPrimers
@TechPrimers 7 жыл бұрын
thanks ainuddin
@raaghaviravisankar1853
@raaghaviravisankar1853 3 жыл бұрын
How is it returning hello it should return hello world right since it specified in service?
@bhargavkommu9423
@bhargavkommu9423 7 жыл бұрын
Hi.. can I request you to give an example on power mock for static and private methods
@TechPrimers
@TechPrimers 7 жыл бұрын
sure. will do bhargav.
@dileepalla6769
@dileepalla6769 7 жыл бұрын
can u post an example on how to perform logging in spring mvc ,hibernate and create a war file and deploy into server and junit test cases and add to hoe to add repository svn or git post one bigger example
@TechPrimers
@TechPrimers 7 жыл бұрын
Sure Dileep. I will do one soon.
@yqevawang1864
@yqevawang1864 6 жыл бұрын
Nice post!
@TechPrimers
@TechPrimers 6 жыл бұрын
Thanks yqeva
@kirann9433
@kirann9433 4 жыл бұрын
how to write test case for multipart file as input for post request parameter
@basmaradadi9155
@basmaradadi9155 4 жыл бұрын
Hi , in ur folder's project in github it missed some files (like the model ..) please check it thnks
@midasama3124
@midasama3124 4 жыл бұрын
It looks pretty straightforward with dummy JSON files, but I would love to see a video with a real database!
@lajos-berenyi
@lajos-berenyi 4 жыл бұрын
Hi Miguel! What you are asking for is not anymore a Unit test, but functional test, what can be done mostly with selenium, or trying the application with postman, etc. The Unit test is testing only a logic of a particular method of particular class. For example in his case we just testing the logic in the methods of the HelloResource class. Here we don't test the logic placed in the dependencies, so we here just mock them and only check if they where called (verify in the video). If we would like to test the logic in the HelloService, we make the separated test class for that, where we only test the logic in the HelloService, etc. So similarly if we use a Dao or Repository for DB operations, in the Unit test we don't really connect to the DB, we only mock the Dao or the Repository class, but we can check if their methods are properly called.
@midasama3124
@midasama3124 4 жыл бұрын
​@@lajos-berenyi Hi Lajos. I really appreciate your extensive and fully descriptive explanation. I am still struggling with these concepts regarding software testing. I get your point that my request gets out of the scope of unit testing, but I just don't see the point of testing simple CRUD operations (at least in my case) on a DAO or JDBC template mock. I would rather just skip that and test the overall functionality on a real database. For the moment, I just don't know to what extent unit testing may become rather unnecessary, and when it would be good practice to implement it.
@lajos-berenyi
@lajos-berenyi 4 жыл бұрын
@@midasama3124 I will write a simple example of CRUD Unit test (the unit test is testing the logic and not the data), for exsmple the Create method: we mock the repository and checking if in the post controller method the repository's save method was called with the correct parameter, and checking the correct answer of the post method. Also we write a test, that if the repository throw an exception (means the data isn't saved to the DB), the post method also throw an exception or check that despite of saving exception the post method doesn't throw an exception but will answer with not 200 status, but with some 400+ status and give answer some errorResponse json. This how the unit test works in practice.
@lajos-berenyi
@lajos-berenyi 4 жыл бұрын
@@midasama3124 Here is a code example, what I was talking about: drive.google.com/file/d/1CF8KN9acbWAmFSMzHtfXDVn4-gJTGnX7/view?usp=sharing
@gaurav-javatechgeek935
@gaurav-javatechgeek935 4 жыл бұрын
could you plese provide more vieos like crud operation Mock testing for Rest controllers?
@narasimhareddy0907
@narasimhareddy0907 6 жыл бұрын
Can you provide junit for jdbctemplate with mockito
@1mallareddy
@1mallareddy 5 жыл бұрын
Can anyone help me in, how to add headers, params to the request if possible including basi auth? Any videos/link is appreciated, Thanks in advance :-)
@mudassir3174
@mudassir3174 6 жыл бұрын
Hello sir, please show how can we check for XML
@TechPrimers
@TechPrimers 6 жыл бұрын
sure
@tarunsapra1930
@tarunsapra1930 7 жыл бұрын
Hello guys, Nice video but you have used an old technique to test controlllers. Spring boot offers much easier and more intutive way to test the Controllers. Just use the annotation @WebMvcTest and for mocking the service just use @MockBean annotation and @WebMvcTest also auto-configures MockMvc which offers a powerful way of easy testing MVC controllers without starting a full HTTP server.
@smithdragon6477
@smithdragon6477 4 жыл бұрын
Why not wiremock
@smithdragon6477
@smithdragon6477 4 жыл бұрын
I was struggling with wiremock and this way to mock rest
What is Spock? | Specification Tests | Tech Primers
8:54
Tech Primers
Рет қаралды 6 М.
Spring Security using JWT in Spring Boot App | Tech Primers
1:13:04
Tech Primers
Рет қаралды 138 М.
Пришёл к другу на ночёвку 😂
01:00
Cadrol&Fatich
Рет қаралды 11 МЛН
Electric Flying Bird with Hanging Wire Automatic for Ceiling Parrot
00:15
Perform HTTP GET and POST Requests MockMvc to Test a @RestController
10:11
Mockito JUnit Example
18:53
Telusko
Рет қаралды 330 М.
Spring Boot with HTTPS Example | Tech Primers
14:50
Tech Primers
Рет қаралды 83 М.
Spring Boot - Testing basics
16:05
Defog Tech
Рет қаралды 76 М.
REST API Documentation using Swagger2 in Spring Boot | Tech Primers
26:27
Profiles in Spring Boot | Spring Profiles | Tech Primers
14:13
Tech Primers
Рет қаралды 72 М.
What is JUnit? | Why Mockito?
14:38
Telusko
Рет қаралды 369 М.
Пришёл к другу на ночёвку 😂
01:00
Cadrol&Fatich
Рет қаралды 11 МЛН