Code Coverage Using Jacoco, Maven & Jenkins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys this is chidanan from crutch info tech ng and i welcome you to this short demo and tutorial on code coverage code coverage using a freely available tool called jacoko jacoco is a free tool which can get us a very very important metrics for any of your projects as long as it is written in java and it's much simpler if at all your application is built using a tool called apache maven so what i plan to do is pick up a very very simple java application which is uh written in java of course and then it is built using a tool a build tool called as maven i'll show you how easy is it to configure this particular application to use jacoku plugin and then get a very very beautiful report that specifies a very very important metrics of your project called as code coverage then i would use jenkins as my continuous integration server and then show you how the same set of steps can be carried out to get a similar report on jenkins all right first things first let me show you uh what is this plugin that i am talking about so if you just search for jack or go you will find that it's a very very simple tool uh freely available but the only thing is it runs only for java project so if your application is written in java and if you're looking for a very very simple tool that gives you only the code coverage uh at the bare minimal part of it and so this is a tool that you can very very easily plug-in there's a lot of information about this tool as to how to plug it in um how to integrate this with something called as a maven it's got a maven beautiful maven plug-in using which you can plug it into your application code and it tells you how to put your dependency within your maven form xml and how exactly to get the reports and stuff like that you can look at all of this in detail but what i want to show you is a very very simple application and then talk a little bit about what exactly is code coverage all right so this is my application this is a github repository very very simple one at that there's a source folder under this there are two different folders or packages one is my main implementation or the application source code the other one is all my test code or the unit test code so if i go into my application code there is something called as a jacko example under com training example and then it's got a simple um class called palindrome so most of you would know what exactly is a paladin it's nothing but if at all string if you reverse the string if you get back the same result such type of strings are called palindromes so this class called palindrome has got a simple method called east palindrome which will accept a string parameter and check whether a particular string that is input to it is a palindrome or not so if you see the source code of this uh the whole logic to do um this palindrome testing is all written out here now assuming that this is a pretty complex unit a unit part of your code for which you would need some testing to be done so what you expect is your application developer to write lot of unit test cases which cover basically the exact functionality by passing various types of parameters to your particular application these should ideally contain all those borderline cases where you pass all kind of dirty strings numbers numericals or some string um which when you whatever wrong things that you can expect to go wrong with your application all kinds of such data should be passed to verify whether your application code is um working correctly as expected and handling exceptions the way it should be all right so that's where in you would have to write a lot of test cases and um though this is like a kind of a prerequisite for many of your organizations many people don't write a lot of test cases because developers don't feel that there's a need to check their applications because they had a look at it and everything looks perfectly well for them there is nothing that can fail with their applications that's the way most of the developers would look at their core it's bug free kind of a thing however having said that there is a test case that is written in the test folder palindrome test so this is in the test package so if you look at the test cases that has been you know used or the data that is passed around with your application to verify your application code there's only one simple test case that is put in here so it's just invoking the palindrome and ease palindrome it is just passing a null string now assuming that this is your application code you as a project manager would really be interested in figuring out uh how much part of your code is actually covered by your test case data that is passed around it now this is a very very important matrix for any project so that it's a like a feel-good factor that tells you that look i have so much of lines of code and i have 60 50 30 or 40 of the coverage so that gives you a very very good comfort level before you start you know ci cd and all the other parts on top of your application so now having said that now this is my source code this is in a git repository uh the most important part of this project is this palm.xml file i will show you how exactly to plug in your jacoku plugin so if you see this there is this in the dependency part of it you specify that you would need a jack plugin so here's the depends dependency part i'm specified um uh the g-unit test cases and in the plug-ins part sorry you don't have to specify it in the dependency uh if you put it in the plug-in part that's sufficient so in the plug-in part you're going to specify what exactly is this plugin that you're looking out for what is the actual artifact id and the group id and what version of the plugin do you need and after that there are multiple ways of tying it out into different goals and for me i would like to tied it to one project phase which is nothing but a prepared package phase so if you know maven there are multiple ways in which multiple targets based upon which you can run your maven or the goals in which the maven can be run so i am trying to invoke the plugin only when a packaging is done packaging is the topmost goal or the phase of any maven project so ideally i would want all the reports to be created only when the application is perfectly compiled run with unit test because everything passed and your application is ready for packaging so this is a simple setup that i have and let me quickly get started on the demo part of it all right let me open up a command prompt and um let me make a directory check oku demo and let me get into jacoko demo all right so i have a simple windows box on which i'm trying out all these things i have java and maven that is already set up on my particular box and also git so first and foremost i would need my git repository so let me go ahead and clone my git repository i'll use the https url any url is fine for that matter i would say git clone all right so if i do a dir with the same name as my application source code repository name i have a folder that is created so all my all my source code along with the palm.xml is all here now i have already set my path and you know java m2home and all that stuff these are all the prequisite stuff for getting your maven and java uh up and running so let me just show you what is my m2 underscore home i already have maven this is m2 underscore home is environment variable specific to my maven i will also show you what does my java home point to so i have got gdk8 as well as maven 3.6.3 installed you can look at some of my previous demos which tells you how exactly to configure all these things so if i say maven iphone iphone version it should typically kick off my maven and then give me some sort of a version yeah it shows me maven 3.6.3 is the version that i have and the corresponding java java is required for me one to run okay so i have jdk 1.8 all right so if i do a dir i have a formula all right so all that i got to do is i don't have anything else installed in in terms of checkoco or anything like that all that i've specified is in the palm.xml where i'm referring to the checoco plugin and the version so i will just do something called mbn mvn package all right so this will compile the package first do the unit testing if everything goes well then go ahead and package the whole part of it so when the packaging happens the jacoko plugin gets kicked up and then it will go through your whole source code and then show me what are the kind of reports that it would kind of generate if you see here now jacoko plugin got kicked in and it is running jacoko.exe and then it's analyzing there's only one class that is there and um that's what it has done so let us look at what exactly was the report that this guy created uh checkup demo check out example all right so as and when you run any of these maven projects you will find a target folder target folder is where all the source i mean all the um artifacts that are generated by the build gets created and stored in case you don't like any of this you just go ahead and delete the target folder that is what um mvn claim would do so inside my target folder there is a site folder inside my site there is something called as jacoco and you would find something called as index.html or if i open this index.html file with one of my browsers you'll find a very very beautiful report that tells me how much of my instructions has been covered how much of my branch this it does a complete thorough analysis of your code if you know the complexities involved in ah analyzing a particular application in terms of the branches there is there instructions that is there the parts that is there and cyclomatic analysis of your project you would understand this much better but thats not the intent of this demo so i already had some source code i already had written some test cases so how much of my code has been covered with my test cases that is the code coverage report that i'm looking at so if you see this very clearly it says the code coverage is only twenty percent the rest of the eighty percent of code is all missed out so let me look at uh the beautiful report palindrome uh east palindrome was the particular method that was there if you if you remember my source code so in the east balance room it gives you very very beautiful report as to what got covered and all this red thing shows that these are all been missed if you remember the source code for the test case that i wrote i was just passing a null string and figuring out whether my application is being correctly tested or not so when the source code is passing a null string your application hits only this part of the code this part of the code is never tested so that is the reason why you see very very nice report that very very clearly tells me that only this part of my application is tested and the rest of all this part is not even touched by my test cases now this is a really really bad report for any manager to look at all right so this is as simple as running um even against a source code and then have inside your palm.xml have a reference to jacoko and invoke jacoko in one of the targets or one of the phases of my maven cycle now this the same project let me try to build it using my jenkins and see if i can generate a similar report let me bring up my jenkins instance i'm on a windows box this is the same box on which i had java and mav1 where i ran my previous example so this is my jenkins it's a typical csca server that i use now my intention is to ensure that i pull the github repository from my jenkins and then try to run the jacoko plugin and get some reports on it as many of you would know jenkins doesn't really have to worry about all those things because the plus point or the most beautiful part of jenkins is that god jenkins has got all types of plugins to talk to anyone and everyone so what i want to first check is is there a plugin that is available for for jenkins to talk to the chacoco um czecho tool so let me just search for jacoko and there looks like there is a tool if i right click on this and um open their new tab it shows information about this plugin this plugin is pretty um it's an old plugin has been there for quite some time and if you see the number of downloads and stuff like that looks like a popular plugin that you can kind of trust and use up all right so what i will do is i will just click on this one and i'll say install without a restart i'm on a pretty slow network i'm having some network issues so just bear with me for some time because the plugins are installed or other plugins are put out on some of those mirror sites it will take some time for the plugins to get installed and the beauty of all this plugin management in jenkins is that whatever dependency your plugins would have all that is taken care of by jenkins so let me just wait uh for this typically shouldn't take more than a few seconds but since i'm on okay there you go i'm on a little bit of a slow network so it may take some time so the plugin got installed all right now let me put up a job so let me call this as a jacoko demo job um this is a freestyle project though it is a maven project i will kick off my maven scripts later on i will for now i'll just give it as a freestyle project and what i want is the https url of my github repository all right so this is my repository uh source code this is a git source code and i don't really have to give out any credential because this repository of mine is a public repository but nonetheless i have this already there so that's my github repository i don't mind the master branch all the dependency for my project is mentioned in the bom.xml file so i don't really have to worry about doing anything else other than possibly checking how exactly to put out a report once you know my maven sorry once my maven job runs all right so that is my source code and uh as a part of my build i would need to put up some build steps there are multiple ways of running this um if you see a top level maven target this is also fine otherwise you can execute a shell script all right so i have multiple geo maven versions so this is what i would choose and the goal for me is package all right um now once i package the the czecho reports will be put out so i would need in some way of publishing these reports so that is the reason why i installed my whole uh plugin so somewhere here as a part of the post build action you will find a step that shows me how to publish the report all right so here is it i mean here you find that record jacob coverage report all right most of the things are by default you don't really have to look at any of these things because it's got the path and this is the typical path for any maven project so this is the source code these are the inclusions for the java project and stuff like that don't have to worry anything about any of this stuff you can explore more on this once you get used to this plug-in and see if you want to put up some thresholds wherein you want to pass a build based upon how much part of the code is covered and not covered and stuff like that you can look at all that for now i am only interested in just pulling this using jenkins and then giving out a report and see if this matches uh whatever report that we saw earlier on so that's as simple as you know my job can be all right so i would say save and then let me run my project now so this goes to the github on my behalf and using the credential that you have put in goes ahead and pulls my um source code repository and then runs invokes maven a maven invocation is through my jenkins and if everything goes well i should see a report something similar to what we saw earlier as i said earlier i'm on a real slow network so it usually doesn't take this long all right so the reports are there and um if you come back to the project now you will see a coverage trend all right as and when you have more runs in your jobs uh in terms of you have more and more build numbers out here you would find a trend which kind of looks like a map that shows you know what was my previous number and what is my present number and stuff like that and this is a very very beautiful matrix for any project manager even sometimes the customers to look at saying you know how how much in the previous build what was my code coverage and in the present build what is my code coverage now if you see here this shows you know the build number this is the build number one this is lines covered and lines missed so i can click anywhere that i want if i want to look at the report and see this pretty much gives me back the same report that i saw using just the maven thing run that i had in previously so east palindrome it gives me the same beautiful report all right in short i just demoed how easy is it for me to invoke this jacoku which is free to use plugin for my java projects and generate a beautiful report which very clearly tells me from my devops perspective or from a perspective of my application how much how good are my test cases and what parts of my application is covered and what is not covered i hope you found this tutorial useful and as usual i will leave the urls of my github repositories that i used in in my description section of the video and if you have any questions or something doesn't run for you please leave a message and i will try to get back to you as soon as i can alright thanks for your time and you guys have a great time
Info
Channel: crudsinfotech NG
Views: 17,048
Rating: undefined out of 5
Keywords: Jacoco, Code Coverage, Jenkins Jacoco, Maven Jenkins Jacoco, Jacoco Maven, Jenkins Code Coverage, CRUDS Infotech NG, DevOps
Id: 7FB-evY5L3o
Channel Id: undefined
Length: 20min 52sec (1252 seconds)
Published: Wed Jul 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.