How to test Apache Camel Routes using Camel Test Kit? Camel Unit Testing, Mock, AdviceWith, Test Kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to apache camel tutorial today we will be focusing on testing our objective we'll try to look at camel test kit and as other videos it's fully hands on so we'll be doing a big great workshop and in that workshop we'll be looking at how to unit test your camel roots we will also try to use mocks okay and we'll try to do some um advanced testing using advice with testing starts with unit tests right and a good way to perform unit testing on a camel application is to start the application okay then send a message and once the message has been sent you verify you check the expectation you had set on your test whether or not those expectations are met this is how the camel test kit is used for testing camel provides rich facilities for testing your projects using the camel test kit this kit is used heavily for testing the camel itself yes camel itself is using their own written framework for testing uh the code which they are shipping to us okay the camel test jar is the core test module that includes the junit extension as a number of classes on top of junit that makes unit testing with apache much easier the camel test jar can be used for testing standalone camel application testing camel in other environments required specialized camel test module for osgi blueprint testing you should use camel test blueprint for cdi testing use camel test dash cdi and for camel with spring use camel test spring the tamil cast the camel test kit uses the mock component from camel core and you are already familiar with producer template and flucent producer template that makes it easy to send messages to your camel roots all right um so this is camel core which basically includes mock component and producer template just as i said just now uh okay let's move on and let's just focus on the camel uh test uh two to begin with okay which includes all the j unit extension which i just said so there are nine classes in a small jar file which is called camel test.jar which ships with with camel out of those nine classes are three or four that are intended to be used by uh end users such as us okay and those classes are called test support um the camera test support language support and and exchange support okay so where are those classes let's have a quick look if i take you back to our intellij which is over here okay and i believe the text size is good enough uh all right so if i take you over here so in this package called uh camel test junit this is junit five by the way okay um it has all these classes camel test support it has test support language test support exchange test support there are few um factory classes some other utility classes which we don't have to use it all right let's close this thing so come back to our presentation once again so um as i said camel test uh comes in a very small jar which has all these um you know nice utility glasses so what we will be doing today is we'll be using the class called camel test support camera test support is a utility class which has several utility methods such as you know template it also gives you the the camel context and there are various other methods in in that class which you can use to test your existing or or just brand new routes okay which you are about to publish to your to your customer so what we can do is we can go back to our intellij and let's try to write our very first unit test okay and how do we do that so i was just playing over here and i wrote a class called hello root junit uh test it needs to extend camel test support class which we were talking earlier so if you click on this camera test support you will see there is a little documentation it's a a useful base class which creates a camel context with some roots along with a producer template for use in the test case do not use this class for spring boot testing for spring boot testing you should be using test support okay which we saw earlier that's the important thing to to know here but right now my focus is is basically you know on the unit testing and the mock testing using camel test support so when you extend camera test support one thing you have to do is you have to override this method called create root builder and when you do that you have to return a route builder so what i have done over here is let's say i wanted to test that whether or not if i had a route called direct greeting whether or not can i send a message to two to that particular root so what i did over here i defined a brand new root called direct grating and the intention is as soon as a message lands on this particular root i just want to print it the entire body over here okay so this is how i like to start after that i have written a public uh test method over here and then what i'm simply doing is i'm using template which is again you know coming from uh you don't have to create um producer template or or an any other template it already provided by this particular class called camel test support this is this is really really cool stuff right it literally you know uh reduces all the boilerplate plus all the injection or construction you may have to do in order to get all these you know context plus templates etc so what i'm simply going to do is i'm going to say template send body to this particular endpoint called direct greeting and i like to send a message called team okay i would like to send it one more message to the same endpoint maybe just me okay it has no meaning at the moment because we'll be building some some use cases around it right so i have now written my test it's very simple and i'm just going to execute this particular test there you are okay and i'm expecting if i do team yeah as you can see team has been printed and me has been printed because these are the two messages which i sent okay let's say we managed to test it everything is good we like to probably copy paste this logic over here into a main root class okay so you can you can do that so what i did let's open uh hello root okay so this is the the the root which we created in previous videos so what this root does it has uh from direct greeting as you saw earlier it has id and it simply base prints body first it says hello okay and and it prints whatever you're sending to it and then it has little bit of a condition over here and it says okay if message contains team i like to print i like working with teams if it does not see message team but it sees me or or anything else it printed uh prints something like solo fighter okay that's as good as this okay and after that we are simply finishing uh with with greeting and then we are simply saying bye body make sense now how do i make use of existing root which is sitting in another file what i simply need to do is i'm gonna get rid of all this you know the the boilerplate which we wrote earlier to test here and i'm going to simply replace that with hello root okay because if you notice hello root extends a class called root builder this is how you build build your camel roots correct so i am able to simply say return existing root over here in this overridden method now if i test or or execute my unit test you will notice in the log file okay let's go to our log towards the bottom i okay there it is right first time when i sent a message called teen it's saying hello me oh no sorry it's saying hello team i like working with teams by team that's exactly the logic which we have over there when i sent another message it said hello me solo fighter by me good stuff so what we have done just now we have unit tested our existing route as well as we try to create a brand new route just just to you know when you're beginning your journey of creating roots just you just want to quickly test it by sending some message you can quickly play around it once you're happy you can you know either copy paste to another root or into a xml file or give it to your your another team all right after that what we did we we try to use existing root by sending the message to it at this point you must be wondering how do i verify that that the content which i have sent to my root meets all the expectations you know whether or not because automatically you like to test okay when i'm sending a message it's reaching once or twice okay it has some content to it this is where mock comes so camel provides another another set of libraries called mark as you noticed in the in the previous slide it comes under camel core okay so let's try to make a use of mock in in the in the in the next test so we can leave this particular class over here and we can open another java class which i have just created and as you can see it's empty at the moment let's just copy paste this stuff over here let's just copy paste and what we can do over here is we can go back to our because at the moment i'm testing it there is a little limitation over here because if i try to use mock i need to direct my roots content or what we call producer it should be matching to a mock endpoint okay at the moment we will just define a new route okay which will be saying from greeting and we will be writing to a mock endpoint later on i'll tell you how to make use of existing route which has like no start to finish ie consumer to producer predefined but you still would like to test that route it it will come in the in the uh maybe in a few minutes time right so what we want to do is we would like to do create new root builder very simple like that you simply say from direct greeting okay and what you like to do is you like to redirect this to um something called mock so you can simply say two mock let's say and this is mock greeting result all right what we are going to do over here is now our consumer is direct grating it's going to consume whatever message we send it to it and it is going to write to more grating which is which is our producer now i'm going to read from our producer over here and then i'm going to set some expectations to it and just just verify everything is working as as expected all right so let's just do that so what we have to do over here come over here and then say context dot get root definition sorry you're just gonna say get mock endpoint uh get mock endpoint not context get mock endpoint yeah that's that's pretty much right over here we're gonna say mark colon what we said over here greeting result okay so get mock point is another method which is coming from our camel test support class let's declare that into a local variable okay and let's just simply call it mock now i'm gonna set some expectation before sending the messages to it and what expectation would i like i like to set i like to set mock dot expected message count is two because i'm going to send two messages yeah sending one and sending two you can set very complex expectation as well like no does it contain this uh you know text does it contain this has it met this condition has has it done this bit or that just just just for like you know demonstration purposes i'm simply going to say please check whether or not this particular route has received two messages okay not more than that not less than that because in every case that would be a failure if i say i'm sending two messages but it's only receiving one which means one message is disappearing somewhere if it's receiving three again there is a problem because we are only sending two messages who is sending the third message okay so we have to very clearly specify our expectation that i am expecting fixed number two okay um you have like no another set of method which says at least or something like that you know if i say um expected message count is there all messages await create consumer expected ascending yeah if you go back and check like you know the number of methods there are few like which is at least these many messages or maybe maximum these many messages or something something like that so once we have done that we gonna keep our existing staff which is sending the message sending body sending body and after this line towards the end you are going to ask mock please check assert is satisfied okay assad is satisfied so let's just recap what we did what we did we are sending messages to our endpoint called direct greeting and using mock we are checking whether or not those messages are coming back okay if i now run this particular test cool this is passed which means we have proven here that okay if i send two messages um our our roots are receiving those two messages okay and and the the conditions are satisfied if i set 20 for example just to prove that this this really works okay if i do do that some random number i am expecting this test to fail okay as you can see the spinner is there now because it it has a default time of 10 seconds or 15 seconds which it waits for you know all the messages to arrive now as you can see our test has failed why it has failed it's saying i expected 20 but i only got two which is true because we only sent two messages so which means our assert is not satisfied so this is how you you can you know mock uh your your end points you can just test it over here in your unit test and then maybe you can ship it to your clients or you can put in your xml file or or you this is how you keep on building the roots so far so good now let's just move on to the the advanced bit as i promised earlier okay and the advanced bit is called advice with advice with as as name suggests right is basically saying you are testing your roots with some sort of advice why do you want to do that advice with is very useful concept when you like to test your existing routes but what does that mean you have production really code okay you are not allowed to touch touch that root you can't have the flexibility of you know putting let's say mock at the end or or in the middle or or anywhere okay that's a fixed set of route which you have kind of defined for your your application but you still like to test it and only way of testing it send a message and there should be a way of consuming the message somewhere so you can basically verify that that message ie the content of that message yeah this is where advice with comes it is used for testing camel routes that are advised before testing it basically intercepts sending um two to end points it replaces a incoming endpoint with another you can replace it right you can completely replace uh your greeting with something else and then you try to send to to that particular uh end point it can be really useful like you know when you have some sort of external components database message messaging or or file system or whatever you can replace that bit and rest of the route can continue it's it's really cool stuff it's just that you have to play with it but i'm just gonna give you very high level information that you have existing route you can you can amend that existing route in in several ways to test it okay you can take out or replace nodes of the root you can say look i have a you know a b c d you can actually say you know get rid of b just assume b is probably writing to some some some audit table you don't want to to to test that so you can actually replace that at runtime using the using the test okay uh you can insert new thing what it means is let's say you have a route you would like to do some sort of you know wire tapping to it you advise with allows you to do that as well that's really cool stuff okay um you can you can mock endpoints too okay let's go back to our intellij and let's try to uh build something very interesting so what we can do now is we can create another file there it is okay uh let's start from the beginning just to understand actually this is i was playing with earlier before you begin testing with advice with first thing you like to do is you like to override a method called is used is use advice with why do you want to do that thing is if if you do not do that camel will when you start the test uh camel will start the routes as soon as it sees like now that you are trying to modify a route it will stop the existing routes and it will try to do the new bit that can cause so many problems in your application because you you wouldn't be able to load the context properly so this is this is a way camel recommends that you return true so it knows that it doesn't has to stop the roots but just you know take the advice and and and inject it okay let's see if we can create something useful over here uh yeah as you can see over here overrides when using advice with and return to this helps knowing advice with is to be used and camel contacts will not be started before the advice uh with takes place this helps by ensuring the advice which has which has been properly set up before the camera context is started otherwise you know it it gets confusing it starts it it stops it and then it try to inject it and then you can you can have some unexpected uh behavior from your application all right let's come back over here so that's the first thing you will be doing it and after that i had existing root hello root as you as you noticed before okay so i'm simply going to override this method create root builder and return our existing hello root in our test method what we have to do first thing you would like to have to do is you say context that get root definition so you have to pull the root definition of of your root and that can only be pulled based on the id and i already have id for my my root so if i take you back to our hello root class as you can see when we defined our root we have given id called greeting and that's the id you will be using over here okay so you say context get me the root definition based on this particular id once you've got the the root now you can start kind of you know building your advice with so you say advice with which is again uh you know uh provided by the camel test support class you pass the route okay so so advice with can take over the route and you pass the contact so it can you know pull all the necessary information it needs after that you will be saying you know what i like to do this bit on the existing route and context what i'm doing over here is i'm saying we've add last so there are several methods let's take a look at the documentation if it makes sense so this is the documentation for advice with there are several things you can do you can replace from with so you can as i was saying earlier you can actually replace the existing route with something new you can weave by uri okay you can weave by id you can we buy two string by type you can weave by at the end uh or you can do it and do it at first what is weave over here is so it's just basically a term you know they they're using to inject something or amend something you know naming is this difficult thing they could have used the word amend or edit or or whatever but they decided to use this which is which is doing the the the same thing so what i am thinking of doing here is i'm saying i have my root okay which is simply direct greeting and it's finishing with you know uh another root this is a producer over here and it's simply saying by uh body what i'm thinking of doing is at the end of this route i'm saying add another route called mock finish grating just remember we are not touching our existing route called hello root what we are simply saying is it's kind of it looks like continuation from here like you have from to from finishing over here it's pretty much like you know let's say it is it is very much like a continuation of saying here that dot 2 something xx but if i have to do that i will have to modify existing route i don't know i do not want to do that so what i'm simply saying is using the advice with i'm saying at the end of the route please add this new route called mock finish grading so i can set my expectation i can check the behavior of the existing route really really really powerful stuff okay this is amazing that's exactly what you wanted to achieve right from the beginning so once i have done this advice okay we can minimize that because we are not going to use it after that you have to say context start now you are ready to start the context so when camel starts it knows okay you know what this is the new route i have to add this thing so it can see it as a one single root after that if you remember we did get mock end point this time we'll be reading mock finish greeting which is the new route which we just defined mark i'm gonna set the expectation of one and i am going to send the message to direct grating which is written in the hello root okay and i'm simply sending team and i'm just making sure it is satisfying okay the condition is satisfied ie it's receiving one message let's try to run this brilliant okay our condition is satisfied so what we have learned today we have learnt how to test camel roots using camel test kit okay which is uh over here on the presentation okay and we also looked at camel core okay and we used some basic concept by starting by unit testing then we did mock testing and then we did this this super advanced use case using advice with i hope you like the videos okay um please subscribe if you have not and leave me comment at the bottom tell me what else would you like to see and i hope to see you in the next video thank you very much
Info
Channel: Saggu
Views: 406
Rating: undefined out of 5
Keywords: Java, Camel, EIP, Tutorial, Code, Kubernetes, Testing
Id: 3XA1i4eX_F8
Channel Id: undefined
Length: 26min 44sec (1604 seconds)
Published: Sun Nov 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.