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
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
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 ?
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
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.
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?
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.
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
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!
@@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.
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.
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.
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()
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.
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 ?
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
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)
When I search any topic on youtube and I see Tech Primers' video on it, I click it immediately.
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!
Thank you for the wonderful presentation. I believe you are a life saver for many victims who struggle to understand Junit test cases.
nice one. Its very crisp and to the point. Thanks for the nice video. Keep rocking with more videos like this!!
This is exactly what I've been looking for! Thanks for making this!
You are one of the best teachers on youtube! Love the content it's been really helpful!
Thanks Miguel. Glad that was useful
Thank you a TON.
This was literally what i was looking for.
Clean, detailed and precise tutorial.
Respect.
A perfect, precise, and clean tutorial.
Great video mate! Appreciate the time to share it with us!
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
Solid and crisp explanations.
Very intersting...👍👍👍👍👍
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
Glad its useful Rakesh
Can you please cover these tests with Junit5? I think lot of these things are changed.
Thanks a lot, it is very helpful for me to understand clearly about the testing from Mock MVC.🙏
Guys the test cases described here are called integration test cases or unit?
Thanks for the video mate
Awesome Tutorial.
how to test the Repository class methods. please post a tutorial.
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
you will have to mock those system dependent responses or you need a Continuous Integration server which is based on suse linux env.
@@TechPrimers thanks a lot.you are awesome.keep up video making
Hey can you please write some junit for post methods as well
Testing with Profiles?
Very good Explanation of basic concepts great for beginners to start with.
Unique and awesome video. Please can you create a playlist on JUNIT testing with some complex REST Example.
Hi , why you didnt have w service layer in ur project
While running test case in eclipse getting socket connect timeout error how to resolve it?
Very nicely explained.
Found Useful in 2020!
What about if the HelloResource are using a real service? How do you mock that?
Love the content. Simply brilliant
Its throwing NullPointerException on this mockmvc = MockMvcBuilders.standaloneSetup(helloController).build();
mockmvc is coming null.
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 ?
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
In my STS just gives "NullPointerException". I have the same code but the result is the same
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.
@@Andrewtmcb Hi Andy I am having the problem. I did everything as mentioned in the video but I am getting Null Pointer Exception
Nice and simple introduction.
Really Awesome video. Learned a lot.
Good Job man.
Very nice Tutorial, was wondering about Testing Crud operations the same way.
Could you make a new video using junit 5? I get stuck at initiating the test class
Failed to load Applicationcontext why I’m getting this error?
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?
Nice video for mockito.
How to mock a end point of a controller which involves a interface.
looking for same.... any suggestion?
Getting NullPointerException, why> and how to solve pls explain
how do we load properties file
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.
Very good and detailed explaination
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
thankyou , it was really help full, could you please provide some more videos for this
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!
This test applies for all controller,service and repository layers?
Really helps lot #KnowledgeSharing thank you @TechPrimers
Is it possible to write test cases for SOAP endpoints?
if Yes, can you make a sample video for that as well?
Thank you
Sure will do mate
@@TechPrimers Thank you
@@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.
im geting the error "the method get(String) is undefined for the type HelloResourceTest" in line 36. same for "status()"
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.*;
Good tutorial. Thank you !
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?
Please notify me at shrumsowen1@gmail.com
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?
If you are using Spring REST, then you can use it the same way
Very very helpful tutorial video..
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.
Yes Brian. In that case you can have the JSON value separated out in a file and compare the content
@@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
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.
Perfect🔥
Thanks for this video.
how write test a contoller that calls a service method
You can autowire the service in test class.
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()
Hi John, I have made a video on mocking the Service methods. Check it here: ruclips.net/video/Ijj5RLSAcYw/видео.html
How about for nested json?
you can try this: stackoverflow.com/questions/15371022/springmvc-mockmvc-jsonpath-compare-list-of-strings
can you use MockMvc to test an application which is not built using Spring
NO
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.
Good explaination
AspectJ for AOP will be very awesome if you can do it. Thanks
sure
sir please, extend this video with jpa as well
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 ?
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
thanks tech primers
Thank you. very Helpful
Thanks a lot sirji
Brilliant bro :)
Awesome
awesome
THQ tech primers
Cool. Thnks.
thank you so much
greatt videoo thank youu so muchh
thanks a lot
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());
}
}
Way too many ads :(
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)
Good content
+ranjeet sahani thanks Ranjeet
Why is there a background music? That's annoying af