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

  Рет қаралды 186,025

Tech Primers

Tech Primers

Күн бұрын

Пікірлер: 111
@SushilYadav7
@SushilYadav7 4 жыл бұрын
When I search any topic on youtube and I see Tech Primers' video on it, I click it immediately.
@wallysonruan
@wallysonruan 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 жыл бұрын
Thank you for the wonderful presentation. I believe you are a life saver for many victims who struggle to understand Junit test cases.
@AmitSingh-vb6oj
@AmitSingh-vb6oj 5 жыл бұрын
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
@faheemazfar3059
@faheemazfar3059 3 жыл бұрын
A perfect, precise, and clean tutorial.
@rakeshbhagat123
@rakeshbhagat123 4 жыл бұрын
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
@TechPrimers
@TechPrimers 4 жыл бұрын
Glad its useful Rakesh
@rrafaelpaz
@rrafaelpaz 5 жыл бұрын
Great video mate! Appreciate the time to share it with us!
@miguelrico4338
@miguelrico4338 6 жыл бұрын
You are one of the best teachers on youtube! Love the content it's been really helpful!
@TechPrimers
@TechPrimers 6 жыл бұрын
Thanks Miguel. Glad that was useful
@altjedan7067
@altjedan7067 5 жыл бұрын
Thank you a TON. This was literally what i was looking for. Clean, detailed and precise tutorial. Respect.
@madhug82
@madhug82 3 жыл бұрын
nice one. Its very crisp and to the point. Thanks for the nice video. Keep rocking with more videos like this!!
@ovey2214
@ovey2214 2 жыл бұрын
This is exactly what I've been looking for! Thanks for making this!
@123rubyg
@123rubyg 3 жыл бұрын
Solid and crisp explanations.
@jasper5016
@jasper5016 3 жыл бұрын
Can you please cover these tests with Junit5? I think lot of these things are changed.
@aishwarykumar4721
@aishwarykumar4721 5 жыл бұрын
Very good Explanation of basic concepts great for beginners to start with.
@Ravikirankada
@Ravikirankada 5 жыл бұрын
Unique and awesome video. Please can you create a playlist on JUNIT testing with some complex REST Example.
@yashobantamajhi8626
@yashobantamajhi8626 6 жыл бұрын
Nice video for mockito. How to mock a end point of a controller which involves a interface.
@weekendhoppers
@weekendhoppers 5 жыл бұрын
looking for same.... any suggestion?
@kvchahar10
@kvchahar10 3 жыл бұрын
Thanks a lot, it is very helpful for me to understand clearly about the testing from Mock MVC.🙏
@anand2009ish
@anand2009ish 3 жыл бұрын
Very intersting...👍👍👍👍👍
@sathishrao7926
@sathishrao7926 5 жыл бұрын
Nice and simple introduction.
@mohammedshabbir1347
@mohammedshabbir1347 2 жыл бұрын
Awesome Tutorial.
@MrLangam
@MrLangam 6 жыл бұрын
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!
@motolola
@motolola 6 жыл бұрын
Very nice Tutorial, was wondering about Testing Crud operations the same way.
@veerendrabanka9907
@veerendrabanka9907 5 жыл бұрын
Very good and detailed explaination
@gaurav-javatechgeek935
@gaurav-javatechgeek935 4 жыл бұрын
thankyou , it was really help full, could you please provide some more videos for this
@abhilashsulibela1414
@abhilashsulibela1414 6 жыл бұрын
Love the content. Simply brilliant
@muks
@muks 6 жыл бұрын
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
@Wind-on-myFace
@Wind-on-myFace 3 жыл бұрын
Found Useful in 2020!
@muks
@muks 6 жыл бұрын
Really Awesome video. Learned a lot. Good Job man.
@LearnWithAbubakr
@LearnWithAbubakr Жыл бұрын
Very nicely explained.
@pratikmukane7631
@pratikmukane7631 6 жыл бұрын
Very very helpful tutorial video..
@balank2367
@balank2367 3 жыл бұрын
Thanks for the video mate
@carlchan5931
@carlchan5931 6 жыл бұрын
Could you make a new video using junit 5? I get stuck at initiating the test class
@Saurabh2816
@Saurabh2816 3 жыл бұрын
Guys the test cases described here are called integration test cases or unit?
@ranjan098
@ranjan098 2 жыл бұрын
how to test the Repository class methods. please post a tutorial.
@Harsh_H_Patil
@Harsh_H_Patil Жыл бұрын
Hey can you please write some junit for post methods as well
@e.ch.vidyasagarkorada7341
@e.ch.vidyasagarkorada7341 3 жыл бұрын
Really helps lot #KnowledgeSharing thank you @TechPrimers
@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.
@umeshdhaked1703
@umeshdhaked1703 3 жыл бұрын
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
@brianocorner5604
@brianocorner5604 6 жыл бұрын
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 жыл бұрын
@@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
@andrzejokon1517
@andrzejokon1517 6 жыл бұрын
Good tutorial. Thank you !
@SportAnalysisKing
@SportAnalysisKing 6 жыл бұрын
AspectJ for AOP will be very awesome if you can do it. Thanks
@TechPrimers
@TechPrimers 6 жыл бұрын
sure
@basmaradadi9155
@basmaradadi9155 4 жыл бұрын
Hi , why you didnt have w service layer in ur project
@MegaRockingraj
@MegaRockingraj 6 жыл бұрын
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.
@nareshgoud9225
@nareshgoud9225 5 жыл бұрын
While running test case in eclipse getting socket connect timeout error how to resolve it?
@juliusvm6116
@juliusvm6116 5 жыл бұрын
What about if the HelloResource are using a real service? How do you mock that?
@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
@manojvakkala9699
@manojvakkala9699 5 жыл бұрын
Getting NullPointerException, why> and how to solve pls explain
@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?
@bhaskarbanepali8565
@bhaskarbanepali8565 4 жыл бұрын
Please notify me at shrumsowen1@gmail.com
@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?
@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
@nsmaepa
@nsmaepa 5 жыл бұрын
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 жыл бұрын
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.
@magichatake
@magichatake 5 жыл бұрын
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
@monikasinha478
@monikasinha478 Жыл бұрын
Thanks a lot sirji
@ninja_ape
@ninja_ape 3 жыл бұрын
Testing with Profiles?
@privateuse4157
@privateuse4157 4 жыл бұрын
Failed to load Applicationcontext why I’m getting this error?
@mansinigam7976
@mansinigam7976 4 жыл бұрын
Thanks for this video.
@veerendrabanka9907
@veerendrabanka9907 5 жыл бұрын
Good explaination
@santhoshisunaina3364
@santhoshisunaina3364 3 жыл бұрын
Perfect🔥
@BitByteGb
@BitByteGb 3 жыл бұрын
sir please, extend this video with jpa as well
@chetanm898
@chetanm898 5 жыл бұрын
This test applies for all controller,service and repository layers?
@sauravchaudhary7245
@sauravchaudhary7245 4 жыл бұрын
Its throwing NullPointerException on this mockmvc = MockMvcBuilders.standaloneSetup(helloController).build(); mockmvc is coming null.
@SameeraSenarathna
@SameeraSenarathna 5 жыл бұрын
Thank you. very Helpful
@dileepalla6769
@dileepalla6769 7 жыл бұрын
THQ tech primers
@JohnWick-zc5li
@JohnWick-zc5li 7 жыл бұрын
thanks tech primers
@arunnambiar11
@arunnambiar11 7 жыл бұрын
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 7 жыл бұрын
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
@privateuse4157
@privateuse4157 4 жыл бұрын
how do we load properties file
@nehakumar9889
@nehakumar9889 Жыл бұрын
thank you so much
@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.*;
@pramodar6437
@pramodar6437 6 жыл бұрын
Cool. Thnks.
@rijwanansari6999
@rijwanansari6999 5 жыл бұрын
Awesome
@1sbollap
@1sbollap 6 жыл бұрын
can you use MockMvc to test an application which is not built using Spring
@indranilbanerjee7870
@indranilbanerjee7870 6 жыл бұрын
NO
@erickloningo2252
@erickloningo2252 5 жыл бұрын
awesome
@Rahulkushwaharahul
@Rahulkushwaharahul 6 жыл бұрын
Brilliant bro :)
@raghadabugharbieh944
@raghadabugharbieh944 5 жыл бұрын
greatt videoo thank youu so muchh
@KK-tz5fr
@KK-tz5fr 5 жыл бұрын
thanks a lot
@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
@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.
@JohnWick-zc5li
@JohnWick-zc5li 7 жыл бұрын
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: kzbin.info/www/bejne/f5vNZoWCiKaWj9k
@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()); } }
@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)
@agnihotriashish
@agnihotriashish 4 жыл бұрын
Way too many ads :(
@ranjeetsahani9207
@ranjeetsahani9207 6 жыл бұрын
Good content
@TechPrimers
@TechPrimers 6 жыл бұрын
+ranjeet sahani thanks Ranjeet
@chrisnolan8263
@chrisnolan8263 5 жыл бұрын
Why is there a background music? That's annoying af
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
Пришёл к другу на ночёвку 😂
01:00
Cadrol&Fatich
Рет қаралды 11 МЛН
Java Unit Testing with JUnit - Tutorial - How to Create And Use Unit Tests
21:35
Spring Batch in Spring Boot | CSV to Database | Tech Primers
41:31
Tech Primers
Рет қаралды 261 М.
Spring Boot Unit Testing With Mockito - Mocking Explained
15:30
Teddy Smith
Рет қаралды 30 М.
Mockito Tutorial - Mocking With Junit and Maven
1:11:53
in28minutes - Get Cloud Certified
Рет қаралды 549 М.
🔥 Master JUNIT in single Video | JUNIT Crash Course | Hindi
1:32:34
Learn Code With Durgesh
Рет қаралды 60 М.
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17