Junit testing Interview Questions & Answers with tutorial in Spring boot Java | Code Decode |Part -1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to code decode today in this video we will be covering some junit interview questions please like share and subscribe to support us and we are setting a like target of 500 likes being in any project for sure you need to cover what all logics you have written in your project so junit helps you unit test your java code so junit is java codes unit testing now in java every time we try to segregate our code into multiple units so rather than writing 100 likes of code at a time what you do is you divide your 100 line of code into 5 or 10 simple code chunks and call that from your main method so that's the basic rule we always apply while writing a code in java never write 100 line of code in one single method rather divide your whole chunk of method what it wants to go into simple units and chunks and test them individually so j unit is a testing framework it is used for unit testing and it is a process of testing individual functionality known as units in java we always divide our whole long function into multiple small stand-alone units now if you have written that you need to test also right you cannot leave everything on tester to test whether you have written a logic right or not it is your task also to fulfill all the test conditions for which you have written your function so to test your units you need a testing framework and that is a unit for you for the java code now it is done by writing the java code that automatically invokes the code that you have written and test your logics into it now let's quickly start and implement a junit first this is the junit demo that i have created already for you so that we don't waste our time if you can see this is something very similar to that we have done in crud demo in spring right what we have done is we have created a controller that controller is going to fetch ploys when you give an id to it so there is a database back there when given an employee id like one it will return you all the fields corresponding to that employee and the entity that we have used is this employee the table name is emp when you are giving an id you will get your name and other other things that is related to employee currently just for demo i have name here now here is our service this is the interface and this is service impl which implements your interface and this controller internally calls your service get employed by id method so this get employee by id method is called what it does is it auto wires your crud repository and finds the id by the id that you are passing to the controller here you are passing an id that is passed from your controller to your employee service impl and here it fetches the employee from the repository now you put a business logic here if the name is code then you have to throw an exception else return that employee as it is and if if element is not formed with that employee id throw an exception no such element found in the database this whole thing we have already covered including the exceptions custom exception handling in spring correct playlist already i will give that link in the description below today our task is to unit test our unit functionality we have created a chunk of code lines here and these code lines does one function standalone function to fetch an employee from the database and check whether it is a code if it is code we'll be throwing an exception you are not allowed to access any details of this employee code because it might be a admin user or some restrictive user you don't want to give access to an employee whose name is code now this is the functionality we have implemented here in our java code you need to test so how do you test this functionality from line number 20 to line number 35 for that j unit is available now we have this package src test java already when we have creating an spring project whenever you create right click new spring project this package is something you will get by default so this is a src java and this basic test is already given to you here what is recommended in iit world is create the packet structure same as you have in src main java same in src test java so what i want to test is i want to test my service impl class because logic is written here the main logic is written here i want to test this particular class so first of all i have to take the package as it is and create this package here so new package i have created the package here now in this package i am going to create a class a class will be named as if you need to test employee service impl convention says employee service impl test should be the name of the class this is the basic convention we follow all over across iit world whatever you want to test just rename that class with the test here and then annotate it with springboard test because this class is going to have junit test cases for employee service impl class you can name it xyz also that doesn't matter but the convention says that by just by looking at the class name you should know this class is testing which particular java class for you now with that in place you need at the right test annotation to write a method so public void and which method do you want to test so here the method is get employee by id so convention says get employed by id test method this is the convention we always use in i t world when we are writing the j unit test cases so at the rate spring boot test will be the class level annotation for your junit class and the rate test will be annotation used the method which you are going to test with this particular junit class now in this what you need to do two important things with this video you will be implementing end to end everything you need to write in a junit test case so two more annotations and you will be good to go with the annotation parts the first annotation is inject mock this inject mox annotation is going to inject the dependency of spring for the class which you want to test so i want to test employee service impl so i am going to inject this class into my test class so this class is going to test your employee service impl the dependency has to be inject mock now it is a task of spring container to provide you the bean of employee service impl when you are doing inject mock on it the next annotation that is important is mock and why do you need mock if you have already injected the impl i can directly use this right because now it's the task of spring to give me this particular object now with this i can directly call this get employed by id with one l right how much difficult is it it is not right now why do you need to mock so the reason of mocking is i don't want any crud repository to be invoked i don't want my database to be hit again and again my multiple test classes is running so to mock any kind of dependency in the class that you are trying to junit test in that case you have to mock all the dependencies here so if i have 10 auto wirings here i will write 10 mocking objects here so what i am going to mark here is employee crud repository is going to be mobbed that means in reality this repository is never going to be hit all the dependencies in your class that you want to test is going to be mocked here so now when you whenever you're going to call this employee crud repository with find by id this repository or the real database is not going to be hit whenever any call goes to this mock depository you should return a stub so this employee e was going to return from the database now since you are mocking this and the database is not going to return you anything because it is not going to wait you need to tell this test class that whenever you go to this m crud repository and you find something out of it you cannot since hit it i am going to give you the employee object so when when is the ongoing stubbing this is a method called of org mokito marketo so whenever you try to hit anything in the repository with find by id with one l as the id then return what i need to return is an employee object the optional of employee object so i am going to create a common method private optional of employee object why because this particular find by id is going to return you an optional employee so i want a method which returns to an optional employee create employee stub and here this employee strum i am going to create now with the lombok has created a builder here this employee can be built this employee object can be built easily so how do you create an employee with a builder so stub employee equals to employ dot builder now this builder i am going to pass an id as one l and the name as decode b e c o d e and i am going to build it and when you build this with the builder this particular stub employee object will be initiated with one ln and d code as the field values in this particular object now i'm going to return the stub employee i cannot because the optional is expected so i'm going to wrap it with optional simple as that now since i have wrapped the stub employing the optional here when the i was returning null rather than that i call my create employee stub method so it says okay this is the class that i'm going to use for testing your employee service impl so whatever you want to test inject it spring will give the bean of this class to you but if this has dependency on other classes mark it if you don't want to call it i don't want to call my repository in indirect your database so i'm going to mock all my repository calls in the service impl but it says okay you have mocked it you have asked me okay don't call repository okay don't call database but when i go to line number 23 while running and when it line number 23 i need an employee object you ask me not to hit the database from where will i get the employee object the answer will be that since you have mocked it you can use when and then method of makito so it says whenever you are trying to hit a repository just return the stub employee object so whenever you hit the repository and try to find an employee from the database rather than hitting the real database i am creating the swap employee method you just call that method and return the employee object now since i have made sure that here i am going to get an employee object the code will work fine it will not break when this method is called this method is returning an employee object so this get employee by id is going to return an employee object so i'm going to save it somewhere so this employee is tested employee now i will assert equals that this tested employee dot get name is actually the stub employee name so the stub employee name that i have given is decode so i'm expecting that rather than hitting this repository and fetching the employee whose real name in the background is actually code so here in this table if you can see at id one for the emp table the name is code but since i have asked you to not to hit repository with one l and find the name is code rather doing that return a stub employee whose name is decode so now i should assert that yes my actual coin did not go to the repository how can i make sure by checking the name the the stub employee object has decode and the real employee object has code so when i run this i should have assert equals that yes the stub employee is written rather than hitting the real one now how will you run it you are going to debug using junit test case the whole spring application is starting because it has to provide you the application context and the beans so now here the employee id that i have passed is 1l here you have passed it as 1l so here you should get 1l as the employee id and crud repository if you hover above it it can say mokito interceptor is already there and this is a mocked object of employee repository so real repository is not going to be hit and the employee that we are going to get is not this object with code rather it will be one comma d code because decode is something that we have passed here now let's quickly go to the next line and see whether this is mocked object yes it is mocked object why because decode is the something we have passed in the stub object and not in the real table which is code with id1 so great it says employee has the name decode rather than the code if it would have been the code exception would have been thrown if i go to the next line the no exception is thrown because name is decode and not code if i go to the next line the tested employee object that is written is the stub object rather and hence i can assert equals that yes the name is decode if it is assert equals is done the j unit test case will pass and here the green line you will be able to see so that's how you do the basic testing so what all things we have done at the red spring boot test for the class that is testing at the right test for the method that you're going to test convention says always use the real name appended with the test ahead of it in the class name as well as in method here you can write xyz also still it will work here also you can read abcd it will still work but the convention doesn't say approve that now what you are going to test is to be inject mock and all the dependencies in this class has to be mocked here if you don't want to give a real call to it and the third important thing whenever you have to return a stub object in all those test cases always create a separate method create the stub employee once rather than in every method if i have 10 methods here creating 10 step methods will use your memory rather doing that that create one stub employee in one different method and use that stub employee in every test class very wherever you need a stub employee so these are the basic best practices whenever you're going to an interview remember these so either it's springboard test was a class level at the right test was a method level mock in inject i have already told you at the rate before before class after after class ignore a negative test is going to be used in the next video where you need these annotations to be covered in the next video let me know in the comment section now what is junit test coverage what is mokito what is mocking what all things you need to mock all those things we have to cover if you want me to cover all those things in the next video just let me know in the comment section i'll create the second part of it thank you
Info
Channel: Code Decode
Views: 13,166
Rating: undefined out of 5
Keywords: junit testing in java, junit testing in spring boot, junit interview questions, junit interview questions and answers for experienced in java, junit interview questions and answers, junit tutorial, junit tutorial for beginners, junit tutorial in spring boot, java junit testing tutorial, java junit interview questions, junit code decode, code decode, codedecode, junit java code decode, junit interview questions code decode, code decode junit
Id: gy787xImUrY
Channel Id: undefined
Length: 16min 47sec (1007 seconds)
Published: Thu Sep 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.