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
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!
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?
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
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.
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
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
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.
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.
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.
@@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.
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()
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.
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
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
nice one. Its very crisp and to the point. Thanks for the nice video. Keep rocking with more videos like this!!
A perfect, precise, and clean tutorial.
Thank you a TON.
This was literally what i was looking for.
Clean, detailed and precise tutorial.
Respect.
This is exactly what I've been looking for! Thanks for making this!
Great video mate! Appreciate the time to share it with us!
Solid and crisp explanations.
You are one of the best teachers on youtube! Love the content it's been really helpful!
Thanks Miguel. Glad that was useful
Thanks a lot, it is very helpful for me to understand clearly about the testing from Mock MVC.🙏
Awesome Tutorial.
Nice video for mockito.
How to mock a end point of a controller which involves a interface.
looking for same.... any suggestion?
Very good Explanation of basic concepts great for beginners to start with.
Very intersting...👍👍👍👍👍
Can you please cover these tests with Junit5? I think lot of these things are changed.
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!
Found Useful in 2020!
Unique and awesome video. Please can you create a playlist on JUNIT testing with some complex REST Example.
Love the content. Simply brilliant
Very nice Tutorial, was wondering about Testing Crud operations the same way.
While running test case in eclipse getting socket connect timeout error how to resolve it?
Nice and simple introduction.
Thanks for the video mate
Very nicely explained.
Really helps lot #KnowledgeSharing thank you @TechPrimers
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?
Very good and detailed explaination
Really Awesome video. Learned a lot.
Good Job man.
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
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
Getting NullPointerException, why> and how to solve pls explain
Good tutorial. Thank you !
Guys the test cases described here are called integration test cases or unit?
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
Failed to load Applicationcontext why I’m getting this error?
Hi , why you didnt have w service layer in ur project
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
What about if the HelloResource are using a real service? How do you mock that?
Its throwing NullPointerException on this mockmvc = MockMvcBuilders.standaloneSetup(helloController).build();
mockmvc is coming null.
Thanks a lot sirji
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
how to test the Repository class methods. please post a tutorial.
Very very helpful tutorial video..
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.
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.
Could you make a new video using junit 5? I get stuck at initiating the test class
Thank you. very Helpful
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.*;
This test applies for all controller,service and repository layers?
thank you so much
Thanks for this 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
Hey can you please write some junit for post methods as well
thanks tech primers
Perfect🔥
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.
Good explaination
Testing with Profiles?
how do we load properties file
Cool. Thnks.
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
THQ tech primers
Awesome
awesome
thanks a lot
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
greatt videoo thank youu so muchh
can you use MockMvc to test an application which is not built using Spring
NO
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.
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());
}
}
AspectJ for AOP will be very awesome if you can do it. Thanks
sure
Brilliant bro :)
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
sir please, extend this video with jpa as well
How about for nested json?
you can try this: stackoverflow.com/questions/15371022/springmvc-mockmvc-jsonpath-compare-list-of-strings
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
Way too many ads :(
Why is there a background music? That's annoying af