IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we usually have interesting tasks on our to-do list visiting mars or finding more about metaverse no doubt they're interesting and intriguing but there's something else that is urgent and important to ensure your applications do what they're expected to today and tomorrow so how do you do so by writing tests for your code and executing them either manually or part of an automated process does it sound boring or monotonous no it is not let's see how junit and intelligent idea can help hi everyone thanks for joining in i'm your host mala gupta and today i'm pleased to welcome christian steen with us hi christian it's a pleasure to have you present today hi mala nice to have me here thanks for everyone in telugu christian works with oracle on the language tools in the java platform group and if you have ever used junit in your code base you can thank him now in the youtube chat because christian a long time core member of the junior team uh he is also creator of the tool park christian did i pronounce it okay that was yes that's the right pronunciation and he's on a mission to bring java modules to all open source projects so christian for how long have you been working with the core team of junit um do you mean by posing questions and and maybe raising tickets in their uh buck tracker no i don't think so but that's even way back more in the time of the unit four frame time frame but i joined the core team in 2017 so it's about four years now yes and that is a lot of time thank you so much and a little more details before i let christian take the stage uh please use the youtube chat to post your question christian hates to talk to camera for a long time and would prefer more interaction with you all so please do not wait till the end of the session to ask questions christian will take quick breaks to answer your question i and anna koslava from the jet prince team will answer your questions as you post them on youtube and the session is being recorded and hosted on intellij ideas youtube channel so if you haven't already subscribed to our channel please do that now last but not the least if you like today's session which i'm sure you will please hit the like button so that more people get to know about it christian i'll add your screen to the stream and let you take the stage now so here you go all right yeah hello everybody and welcome to this live webinar intellij idea andre unix writing finding and running tests um i prepared no slides mahler introduced me already i'm working for oracle for two months now so this is uh my oracle head off and my journey head on so i'm speaking about how to write tests in 21 um i prepared a repository you can see it online already here under the idea junit webinar under my github repository you may follow on stream live or you may be skipping some parts and coding for yourself i'm going to commit and push um all the things i'm gonna do in the next hour so be my guest pull requests are appreciated if they are live so um go for it you need a recent installation of idea java 17 and drain it five uh yeah but there is no j net five but later more so let's get started jn5 is or was maybe the next generation of junit and the goal was and is still to create an up-to-date foundation for developer site testing enabling many different styles of testing and with testing i have to clarify first i don't mean using a product from a to z or testing all the implications that a product has on some social uh impact or impact on people and health and whatnot testing is here clearly defined as an automated little algorithm trying to invoke a method a system a unit you you name it and then we're gonna um inspect the result and we're gonna see if that one it that was expected or not and because of that without further ado let's get started let's see if the java version is up to date yeah it is and the same for the project let's inspect the project structure yeah we have an um and recent oracle opengdk installed you can choose others you can download them from here intellij helps you there it's a perfect feature to get started right away we're gonna stay in the language level 17 today not using all the new features but hey it's test code and one thing i always do first is route out the compiler output to a different directory inside the local id directory because everything idea does should stay in one single directory so now since we have our root module let's create a new one because as marla said modules modules everywhere and we already got the project module here so we need a java module which is now first taken as a intellij idea module and we need a name for this we're gonna choose a short name that's our basic tests and idea will create a root for this module and the module file location is not the one for drama that's the one right here and like i said before i want all my idea related files in this dot idea directory we're gonna press finish clean up a bit because that was a good guess from idea to have a source directory but we are writing tests so we're going to create a new folder and let's start with a common pattern and mark it as a tech test directory for um test sources route we're gonna choose okay now i'll look into the path that's everything fine dependencies we don't have one just the oracle jdk 17 module sources on top and yeah we got our first module over here and like you have already noticed um we don't have any build tool tonight or today in this hour because ideas fine enough the jdk brings along a lot of tools and we're gonna use those two uh players tonight no extra layer of build tool because actually one if you want to learn something scrap the layers off maybe remove all um players which are not part of that game and try to understand what's going on here all right so we got a idea module we got a directory marker sources now we can start creating our first source file and that's a module info file and idea yeah you choose a good name and something what the module module is something you want to put your packages into your java packages and you want to say which modules under test so which system which class with package and the outermost shell is a module so let's require some module which are already already here we have a lot of modules to pick from java dot jdk dot a lot of modules are shipping with the java se platform but java base is enough for now yeah you don't have to edit it's mandated anyway but let's be explicit all right like i said let's move everything we changed so far under a single commit because we had our first ikea and java module in this comments gonna push it right i'm away close a bit more and here we go everything everybody um who's footing online might update now the project and code with me after that we gonna need a new class and from my um plan we start with a simple program that's how you do it in java right you need the class which is in the test based module so we gonna use test base our package name prefix and we start with a hello demo because example program is too long to work demo with shortcuts so now we're gonna use some very old stuff we're gonna say hello everybody and that's it and and you see those play buttons over here to run a program if i press it and i like pressing buttons intellij starts this demo right so we got our first test actually we wrote some code and we verify we get an output hey that's it that's also a test if you can write one we can write the second one let's do some math demos so what about math of minus one thousand and if this is greater than 0 or equal to 0 i guess we have an error so what about throwing a new assertion error because it's an insertion error that failed as an insertion we had so this program should end here otherwise we could continue and say it's all fine here we go let's run this one it's always true why is this always true so if an absolute is always greater than you so if it's uh below zero exactly let's test again so you see sometimes assertions don't mat what's in your mate in your head and so you have to figure it out i actually wrote it the other way around okay everything works great and only three things are good things let's make a module demo [Applause] so let's assume we are in a module which is called test base and if this equals module demos clause get module name then that's expected so we're gonna update this one and otherwise we're gonna throw [Applause] [Applause] so let's try this and then we have not enough example programs so another one which is not right so let's let's make it more clear what we want to test so this is the expected name that's the actual name and then we are done so now we can see what's happening if our expectation is right we gonna just print out everything is fine otherwise we have the wrong name and then we're gonna fail here yeah okay we have written three tests we verified on the command line that everything works but how do you do this on the terminal of course you can do the same and execute it here or in a size ti server and a batch script and that's all very easy since java 11 you can run single source file code files with just invoking the java launcher just have to find the right directory where the code is in then it's not here it's in this package and yes as expected we see hello world or hello in this case um but now you may say yeah what about running all of them and for this idea got a solution we can create a compound test run we're gonna call all demons then we add all the applications we just created and then we're gonna run all of them and you see the hello math and module demo just executed everything was fine just try out never did this before we have an error on the last one yes we're gonna have an xcode of one and see there it was an error so let me commit this as our introduction files and also include the run targets because we're gonna need them later so how do we store that as a project file we just say here yes there yes done store this one choose the preferred directory and apply we're gonna get some configuration files for idea this is all the same commit i'm gonna check this in and excellent programs so proof runs some demos here we go all right so we just revisited a very old style of using and and writing tests and this is all uh what's about today but let's fix this here fix module demo just want to have um a working demo for the next part which is we're gonna do excursion to jupiter so fasten your seat belts in your rockets and now we're gonna have a new entry point because that was public static void main that was the entry point actually with the string area arguments um string array parameter here it was entropy for every java program and for the next part we're gonna have a we're gonna transfer the module demo um code to a jupiter test because everything testing framework did and does today is basically providing new entry points so let's start with a java class a new one which is in test base jupiter um and now from module demo it's modular tests because we're gonna have some model test tonight we're gonna copy the code from here one by one um jupiter tests which you may learn from the user guides don't need no public modifier we're gonna write some method which is called module name is as base we don't need no arguments and that's it this should work right perhaps adding a test annotation here so now everything goes no yeah an idea has a very good quick fix we could add a unit to the class path but although we could update this here we're gonna take a little a side approach because i prepared all the external dependencies and we're gonna do this by hand tonight i'm gonna check them you need so you don't have to find them yourself which is kind of a hassle without a build tool okay let me create a directory which i create external modules because we want to hand over the control of finding the entry points to an external framework we need to have this framework in our project i'm going to copy some modules i'm going to add them right away to the repository so you can use them later and now ah still the same so what's going on of course if you want to use some external modules or anybody we have to declare it in our own module so these are the modules we are testing with [Applause] so where is it it's not here it's somewhere okay external modules how do you do this we create a new library in the project structure of the idea project we say it's just a simple directory and we want to add it to test base and that's it and here the error is gone now we have the junit jupiter module included and now we can yes import annotation this is our entry point our former main class and now we can just run the module tests here yeah i knew this was coming because only because it compiles apparently it doesn't mean that it run because if you look deeper into the module you see we have a juniper platform dependency and some open tests for j dependency and rpg guardian api dependency so let me copy those over too this is what a build tool should do for you but we now have them all here which consists of all those and we're gonna add them too and restart the test and finally unable to make test based demo module test accessible module test base this is our module does not open test based demo whatever it's a package it's a long package i guess we made something wrong in the setup at least this module wants to reflect wants to see and wants to invoke the message so we have to permit that and because one more thing uh before going on i have to fix this ah see it's inside the job um let's move it over here conflicts doesn't matter okay it does matter so once again because i have a error in the package structure let's ask idea for help jupiter and if we do this we can have package quick fix here we go and delete all the other ones we didn't want to now we have our demos here and we have our module test in the expected package let's run it again and see the same error again another error yes why i don't know let's rebuild the project and try it again ah we moved it but maybe yes this is didn't got the rename notification now we are back to normal we can run this test which we couldn't because it was inaccessible so now we can open this package but as we know that we are in a test only environment we don't have to open all the packages of this module by each by one by name we can just say this is an open test which opens every module every package in this module so if a framework you're gonna use and require uses reflection you usually have to open packages to make it usable and finally we have our module test our single entry point which was a program here and which is now a single test here before asking if there are questions on the chat mala um i will clean up this test a bit and then we gonna dive deeper into the jupiter um api writing test so you have expected an actual and in this uh order so what shall we do to make it more streamlined of course uh christian do you want do you want me to ask the questions now or later probably i misunderstood your comments yeah if there are any uh interesting questions by now we we can just make them answer try to answer them now yeah so first of all everyone is loving the way you are making mistakes and then correcting them because everyone is saying yeah that's me yeah that's how it goes not only live sessions that's day by day where you learn by making errors and then just keep on going and some where green lights appear at the end of the tunnel and there was another um i would say a happy comment which said uh christian is smiling while writing the test wonder we could do the same while we write our test so that was another question that was that was a comment yes that was a compliment and a good one and i would like to mention this to everyone christian has been an amazing person to talk with to present and yes you can make things happy for you if you like christian and write tests that's awesome christian there's one quick question you mentioned about assert equals and um uh you should be aware about the test ng uh framework that we have for testing why why does junit in test ng have different place where we pass actual and expected values to the assert equals method is that to confuse the developers absolutely to to make you all stay focused now i think it's it's uh evolved over time and and what's uh more fluent to the api wars and is and if i remember correctly but i may be wrong the chat will explode if i'm wrong so it's just tame i guess it's invading it for it's it's actual um expected and there's no flip in test ng it's extracted actually right um yes let me just check where are my notes yes and test ngs it's actual and expected and jenny unit is expected first and and just to make it clear in general five it's also expected actual so in this case test ng one and having inspected first this is great but and there's always but if you use ether j you start with essert that actual again and now something expected so it depends there's no specification there is no no no ill intent here no but yeah it is what it is so you have to know the frameworks you're using and and then to keep them keep them doing with it in the case of a third equals it doesn't matter mostly most of the time it depends on the implementation which object is called first to the equals method but let's make this test very small and we're using the j5 api here for now so everything is just working as normal so if any questions raise to just ask them sure and i will let you move forward with your presentation now all right so after this interesting question let's make this test a bit smaller what's a wrong name so this detail message is not very detailed at all so let's make it longer so it's the wrong name and we're gonna have expected [Applause] to see what was wrong and we gonna have expected first and actual last it's gonna replace the forward keys in here and if i enter a wrong name we gonna see yes this is a uh assertion error so we have expected some base one two three but we got test base and this is totally wrong so perfect now what was the first thing the framework got um gave us was a new entry point and of course now we can have multiple entry points in the same class not just one public static void main method the other thing a test framework like jupiter is gives you is a assertions helpers and we just talked about the equals where you put in the expected first and then the actual last so this is here and as we still got our wrong expectation it should fail with something similar we just had yes and ideas even providing a a a comparison window where you can see yeah there's something different and that's great but did you see the change now we have an open test for j assertion failed error which is somewhere inside inside inside this method so let's see where this goes to this is the source code of the jupiter framework it goes into the assert equals method we're jumping down if not equals then we're gonna fail if it fails we have an assertion failed error and which in turn is ah the java length assertion error so nothing changed it's just wrapped many times and the code we got here it's almost the same as we got in our program so let's fix this test and see a green light so we can commit this work step and i have our first jupiter test in our repository so the external module file was not committed yet the module test was added in ui designer man okay be included so let's call this introduce jupiter here we go so let's close the module and the tests and see what's next yeah what but what about our example programs we put so much energy and time into writing those little three applications wouldn't it be nice if the framework we just included execute these applications as tests and you may ask why and we're going back to this later so let's try to pick up those demos and this is gonna work and by including another module which i just prepared i'm gonna paste it in here and this is a test engine i just [Music] provided over here and it's a main runner test engine and the main runner test engine does nothing else but searching for classes that have a public static void main string area method in it and executes them so we got it installed and now we're gonna run the module test again ah nothing happens but if i now press run all tests on the test module we gonna see yes we have jupiter here which executed the test method we just wrote and the main runner engine which found all the old demos we programmed as if working on the console it just executed the main code so without losing any of our efforts we just we included all the programs we wrote here so this is one of the highlights and then the the advantages of the neutral unit platform jupiter is a test engine on the low end on the high end on the api side it's it's a framework how to write tests but if your style is to write example programs that's no problem just put in a test engine that knows how to identify example programs and then it can run it right so um mala any more questions by now because i think that's two thirds of the time now um yes i have a couple of questions i'm unsure whether you want to take them now one so i'll ask a quick question so one of the viewer wants to know whether you would be covering anything regarding testing spring today no no spring today no build tool no other framework so probably uh later you could share any link that you have where anyone who is looking information on that particular topic could just go and take a look themselves yeah um actually this website uh janet.org jl5 provides a wealth of um from from the source code to the wiki over the links into stack overflow where you can ask questions or yeah so that's a very good source for digging deeper into this topic yeah okay so anna is answering the other questions so i'll let you move forward with your session okay all right okay now maybe this is the expected part of this talk we're gonna dive into user guide now i have a local copy of it and you can browse it from the website junit5 or j.org jl5 and everything i just covered was down here in the [Music] ide support package and it was down here at the [Music] dependency meter data where i copied all the dependencies from and put it into the external directory for you and i guess everything is clean no it's not so just let me first so this concludes the the motivation part that tests are just a or test frameworks help you um to have different entry points into your system so now let's talk about what the jaina team thought was a very good way style of writing tests and the user guide chapter two is about writing tests with the jupiter api jupiter fifth planet from sun so there's uh there's the five gun it's it's encoded in the name and we got and saw the test annotation and there are a lot of other annotations we could use and so let's dive into them let's go back to the module test and have another test but this time a parameterized test so what's that that's one test you can um check if you can test an argument a set of arguments yes check if package is present so what we want to do we want to check for left over just fixed we want to get the module of the current clause we want to have the descriptor of the clause of the module and we want to have all the packages which are in this module so these are the packages and these are the action and we're gonna see here we do see it's a set of strings and all our tests is that the set of string should contain the name we just put in so assertions assert true that the set of packages contains a name [Applause] so which one just try it out first it's a module test let's run it we got an exception again you must configure at least one set of arguments because it's a parameterized test okay the platform failed and told us we need a list of values interesting at least one set of arguments so perhaps it should say some source okay the name is one two three and it's all strings inside here let's press this again and here see we have a test method an entry point which is then presented as a single test and now we have a test method which is a container of itself and a new entry point which is initialized with one two three so we gonna have a more you can have one we can have more and those package names are not part of the um of the module so let's fail this test with a better message because we want to see which packages are actually here we know them they are on the left side on the screen but we want to see them in our error messages which are produced now for two so this is the set we got we have and we enter those package names here everything should be green again and now we have a parameterized test for a fixed set of strings so this was already the later um chapter here and there's so many other things it's all um like 20 subsections and it's going from simple assertions to display names nested classes assumptions which are about are aborted or disabled tests conditionally um but i guess we will stay inside the parameterized tests for a bit or mana is there some other um topic we should dive into um so christian there uh there's one question regarding integration testing so are you going to cover integration testing as well or would you be just working on unit test yeah what's an integration test can you ask back no no no rhetorical question because this is all about testing checking little programs so integration testing isn't depends on what you define at the integration test is is two systems two units three units is accountable so no not gonna cover integration testing tonight so it's just um basics of the framework here okay and would you uh so that's a follow-up question and then i would let you to move forward with your session so would you recommend a framework for integration testing can junit do that yeah that depends on what you're trying to do um junit is just a a um framework to write tests and if the code you write does something you you think is in an integration test that that's an integration test yes but it's not a ui testing frameworks there are no browser robot control things integrated there's no database access integrated but there are a lot of extensions that provide those features and they're all on the jl5 website and in the wiki so yeah thanks christian i'll let you move forward with your session now yeah so we are already on the finish line so time is up already let's see what's inside of the [Music] um inside of the user guides so i'm going to skip most of the the the writing tests actually so be our guest read the user guide it's all in there we can copy over more sources we have plenty of sources for parameterized tests you can have enums or even own methods so this not gonna fit into the last 10 minutes but going back to the plan i want to show some extensions maybe this is interesting to to close um so if something is repetitive and the api the 20 chapters i just showed very briefly doesn't help with your use case you are free to extend these this api of jupiter and the entire chapter five in the user guide is about the extension model of jupiter and this is where formerly the competing constructs of jnet 4 runner test rule method rule and whatnot had a very clear limitation and this is substituted that this was subject to reinvented for jupiter with the extension model so let's write a small extension shall we and it's prepared so we don't have to care about too many errors we're gonna have a a sleep extension because it's getting late over here in europe so it's time to sleep and what about if we just gonna wait some seconds before and after each test method each method is annotated with a test annotation so it's inside here the implementation is very easy you implement an extension interface normally they have a single method you have to implement and that's it you're going to run the entire tests and we saw no that was too fast now how do we activate this and from the extension we just saw we just put it on the class extend with the sleep class and we're gonna run it again and then we're gonna see in the preview of the run that we have a longer test run so if you want to do anything before or after a test run or if we look into the tree of the weibull extensions well this is not going to work here right now it's too small let's have a look in the user guide instead there are a couple of extension extension points you can implement and integrate whatever you do whatever you are able to do in in your programming language so um [Music] we briefly touched how to write jupiter tests we very briefly touched the extension module and there are a lot of other like advanced topics how to run the platform how to run a suite of tests programmatically was just introduced but let me close with a interesting part here so value source is one thing we could csv source um so if your data is available as csv file you can put it in here in a file source if it's if it's in a string you can use a text block as well so we have a single test [Applause] and one two three and now we can run this again and now the data is extracted from the csv source file or you just copy pasted it here and yeah this one is not part of the packages let's remove the sleeve over here to make it fast again and yeah that is all i could show in this short hour and i hope you enjoyed the the ride mala any questions to close first of all thank you so much christian it was a pleasure to see you present on junit and i think a lot of people found new love for modules and testing in the session because i'm not making it up i saw a lot of comments out there so thank you so much yeah you're welcome and thanks everyone for attending and asking the questions oh no we have to uh ask a couple of questions just a moment yes okay so we have questions coming in noun how can we test control a class with that new junit 5 what link to study that function do you recommend [Music] what was the topic controller function um how can we test controller class with that new junit 5 oh i don't know what a controller class is in this context i missed the uh coding part probably the person is referring to controller class as we have in the rest api probably i don't know in you know so let's skip that so there's another question that i wanted to ask so um if you know that junit 4 is a single jar file but j unit 5 is complicated why so you have two layers junit platform and the test engine with several modules in each yeah that is basically the the evolution and the revolution part and the evolution was done from jaden 4 to jupiter inside a test engine which was some modules but that's the logic resides in here so this is basically part of what was j4 which is no four modules um and the other framework around it is to find and to execute tests and run tests and and this separation is very good to have in the context of tooling because build tools and ides like idea um they only have to know about the platform and the um and this is very stable and very easy to to to communicate with and maybe next year or the the year later we have a new programming and testing style and this you could encode into an s into an engine and then drop it on to the module path and then everything just works and you get your little green play buttons in the ide and it doesn't matter that's invented next year so it's very future proof at the cost of having a lot of modules it's fair enough and so probably i'll take the last question and then we'll uh just close the stream so is it possible for anyone to write their own testing engine yes of course um it's briefly covered in the user guide it's i think chapter six plug in your own test engine somewhere here you can even test your extensions on your test engine with the engine test kit and that's it you just have to implement a single interface find your tests you're looking for and you have to evaluate them later in the execution path and then that's it so it's very very easy and i guess johannes and matthias just wrote a blog article somewhere on the oracle block about writing a test engine so have a look there thank you so much uh so i think we are towards um we've consumed all the time that we had so again uh christian thank you so much it was an amazing session i really had a lot of fun hosting it thank you so much any closing comments by you um yeah happy testing everyone and if any questions are not answered yet i'm gonna scroll through the chat and answer them as well and yeah see you soon probably you want to encourage people to contribute to junit and if there are any links that they could access to do that that is of course the websites so if you want to help out with a code with a bug report you're welcome every time we also have a sponsors web page where you can help us working together um as a as a core team so thanks for any contribution on there so thanks everyone for joining in for participating asking questions and thanks again christian for presenting the session stay tuned for our next intellij idea live stream and on december 15 we are hosting another session on refactoring by sandro so until next time bye see you you
Info
Channel: IntelliJ IDEA by JetBrains
Views: 5,255
Rating: undefined out of 5
Keywords: intellij idea, webinar, junit, testing, mainrunner, junit 5
Id: ZzF5xuw_xW8
Channel Id: undefined
Length: 60min 36sec (3636 seconds)
Published: Wed Nov 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.