Hi, welcome back. In this video, you'll understand about how to use m unit for doing unit testing and integration testing. So, for demonstrating this m unit I have a very good use case where I can show you various scenarios. First let me describe about this use case. I have a main flow where I have HTTP listener, okay. This listener is listening at what URL slash TEST TEST Okay.
Then, after that I have set payload, what it is doing. I configured to set the payload by extracting a query parameter called as you are l underscore key. So that means if I give a request to this flow using localhost Colon 8081 slash test. Question mark, you are l underscore key is equals to my underscore payload. Okay. So payload will be what?
My underscore payload, then I'm calling a floor reference, this floor reference pointing to choice flow. What does this choice flow doing? Here it is having conditions. If payload is equals to payload underscore one, then we are calling sub flow one else we're calling some photo. So that means if I pass URL underscore key as a node underscore one so now the choice This will be executed and it is referencing to sub flow. What is this doing?
It is setting a variable my variable the value is a variable one. And if the payload is not fair underscore one to four suffered to were suffered with setting a variable, same variable, my variable but the value is my variable two. That means the outcome of this choice flow will be setting a variable called as my variable the value can be either my variable one or my variable two based on the payload. Okay. Now again the request comes to mean flow here I have one more choice what is doing here if you see the configuration the crowd Choice is having a condition what is the first choice conditions whereas what my response to my will be one set the variable value is my variable one this will be executed where am setting the payload is as fast as quote period and score one yes, the payload will be responsible for relevance to.
So, summarize. If I pass the value of URL underscore p as paler underscore one, this will be executed and sub flow called local set variable. It will set a variable my variable value may well be one. The control counts here The responsible way as Constance corpulent core one so let me run this test first. I want to restart to see the console. Yep, I'll wait until it distracts.
Okay, it got deployed. So let's give a request I should get a response from Scott builder and score one. If it was you or something else, I should get response and score buildings. That's it right. So, basically, I want to write a unit test case and integration test case. What is the difference between integration test case and unit test case if I talk about this main flow so the different simple differences in integration test case, see here The first flow the mainframe is calling other for right?
You want the main flow to call the other flow if you are writing an integration test case, but if you are writing a unit test case, you just want to see the logic inside this particular flow. So in unit test case, you will actually mock any external calls, right. We'll see how to mock this floor reference later. So let's see first of all, how to write integration test case. Okay. So if you want to start writing a test case, all you have to do is right click on the flow for which you want to add a test case and simply add a Munich create new suit.
Right. So you start Wait, yep, you can see the perspective is changed. And you can see importantly when I added the amulet modules it got added m unit and M your tools also got added automatically once I created my first so that's great. Now, if you see the amulet test case will be under SRC test m unit and this is the file which got generated right now. See here in every test so, there are three parts first one is behavior on the top, here is execution where you need to invoke the flow which you want to test and validation, we can write assertions. So there are three parts of a test case.
So this whole file, we called as a suit in one suit, that can be any number of tests right now. So, this created with only one test. Now, if you see here in the execution part it is referencing to my main it is referencing to actually my main for which I want to really test Normally, if you see this main flow normally, in this case, whenever any HTTP request is coming, this listener will convert the HTTP methods to a mule event is it such a pleasure we'll create a new event automatically. And what it will it will populate the newly events, payload attributes everything. But remember, whenever we're calling the same flow using flow reference, any inbound endpoints will be ignored and the mule even deadly kuzia so If I'm using flow reference to call from my test case, who is responsible to create a new Levent?
In earlier case the listener was creating a mule event by taking HTTP request, but not from calling the same flow using for reference. I have to create a new Levent add a set of newly went even before calling the for reference. So, what I can do is I can just go to this amulet module. Here there is something an operation called set event which will drag and drop so here I can create a mule. So, as you know, the mule event will have some payload. You can write an expression or dw expression and you can set the payload is it and you can use the values and here attributes.
So as you know even will contain attributes and you can view attributes here. Right? So what all does what all does a mule even contain relavant we'll continue now, payload attributes attributes are nothing but key value pairs. And then what else maybe some variables again here. Here you can add property type is variable, variable name it variable value. So that's all right, I even will continually payload attributes and variables.
You can add variables also like this. So right now in this scenario to test I don't want to set any payload but I want to set an attribute attributes is nothing but map is it So you know how to write a map. So I'll just try to show you for Notepad. So this is a map in dw right? curly braces, okay. tattered versus a map.
And normally when you're making HTTP requests in attributes they will be what? query params. Right. So if we select Set payload, just to show you I'll drag and drop transfer message here. If you just observe here on the left side of transform message, under attributes there is query params. query params is again an app which can continue on to to query params.
So in this map, query params is a key So what I'm going to do is write, really, column. Again, the value is a map that I can have multiple query params, right URL underscore, key, colon, a load underscore one. So we are setting we're creating a map inside which query params is on the map, because you are in Skokie, Illinois for one. So I will copy this and I want to write this as an expression to attribute Okay Oh What happened? I think got deleted sorry. Drag and drop city went again before for reference.
And here are four attributes and paste a URL copied. Yep, it appears like this. But if I go to XML and see I can be perfect Oh, no. You're again go to that salmon fish at Yeah, this is what I want to paste. Actually a small thing I missed he's also should be in Singapore. Okay, no, I will copy, copy this and go here and paste it.
Okay. But this is a mule expression, which has to be dw expression which has to be evaluated right at key within that square bracket, then only it will be evaluated. So what I have done is I've created a muli whatever city you live in with these attributes, and then I'm invoking the floor reference, the flow will be called, what is the output of executing the flow in this case? I'm expecting the pillar to be response underscore payload underscore one. I want to assert that the payload is this one. So what I will do is here in the sessions part validation part I can add an assertion.
So, if you see here under m unit tools assert that there is an assert that I will just drag and drop assert that I read an expression is and the message right. So who I want to assert that the payload, I want to assert the payload so I write hash a lot easier is actually m unit tools are having a lot of operations. So, if I want to use anything, how to use hash, young unit tools colon and I'll press Ctrl Space c these are the operations which are available in M unit tools. I can use it in my dw L. So m unit colon colon, they can use any of the function equal to I can use equals to, I just paste this response on Scorpio underscore one. So this one I must setting amyl tolls equals to EZ Pass underscore filter underscore one The reason I just save it.
Actually, if we just come back here and check, there's not an error actually, if we save it there, our method is gone. And here I'll write a message. If this assertion fails, I'll say just try to error My payload is not as expected. Okay, now I will run this run em unit. So let's observe actually for Alina, so do you want the mid server run? Is profit and run em unit suit.
So let's wait. Yep. mld starting that application internally you just have to wait On the left side, you can observe the only one cases. And you should observe that this succeeded because our session is success. Now, all I know is that it just to just make less failure, I'll try to assert that the expected value is something else other than the size. As I said to this one, now run the test again.
Running this so there's only one case again, X sub so what will be the result of this execution? Hmm. Can I expect the test to fail? Yes, the test failed. And you can see here The reason actually when I will click if I click on it if I click on this maximize, I'll maximize this window. Yep.
You're if I click on this one, the assertion error is displayed. payload is not expected expected response on score, but it was response on score failed and score one. So yeah, as expected the assertion failed assertion fraud and error right. So now we have written a simple test case one test case. Let us see A test case. Now we want to write another test case where I want to verify actually, that supply one is called Actually, we can see after I run my test case, you can see tick marks, the control went into this one, right.
So here, it went to this one, this one, this one and so once you're on the testicles to show him that Okay, now, what I want to do is I want to make sure that sub flow one is called whenever I invoke main flow, I want to validate that sub flow one is called. So I can write one more case for the scenario. I can directly call the same flow m unit, create and save it first. I'll close and open ones okay. Now what I want to do is I'll right click on this m unit demo flow. Right click and create new.
Yeah. So again, you can see that in the same XML, one more case has been added. Now, here also I want to set an event with same values. So I'll drag and drop. In m unit, I can drag and drop set event. And what is event I want to configure attributes just like earlier.
Go to XML and check. Oh an XML again. It's Like this I will paste the actual value whatever I want to paste Okay, I'll set an attribute. Now I want to verify a call to sub flow. So, what I can do is I can drag and drop verify call I want to verify on. So, in the validation drag and drop a call to what to which processor you want to verify a call to which processor Actually, I want to verify a call went into sub flow right.
Am I calling? If I want to verify that the call went is of law I need to verify that the call went to this floor FM's whose name is supply one is it sexually section if you cannot verify a call to the flow we can verify a call to flow reference That means the half was called right. So, I want to verify a call to processor whose tag name is new column. flawed as ref. I want to verify a call to which processor, a processor whose tag name is meal, column, flow dash. And with what attributes I want to add in when there is an attribute, name, name and it's valueless sub flow one.
I want to actually validate that the call went to a processor with this tag name and an attribute name whose name is whose values suffer one is it Yes, see his actual file I want to make sure that the call goes to this flow calm graph, where name values sub flow one name is an attribute and its value sub flow one. So I configured with a call and let me run this suit. Actually, there are two cases right now. First case should fail because I intentionally made it to a set wrongly. Second one is a very free quality pass. Hmm, yes, you can see that.
Oh the first one and Second one assertion payload but first we're not saying that error message is saying you call the function Oh, the mistake good good to do the mistake actually index Amal. I should wrap this insert the hash square back at night I want it to be evaluated right. So good. Hash runs decorated Okay. So now I should be able to run it again and, and I'll change these assert also assert that again as expected. So then both of them will pass.
Hello sir, exactly. Okay, now if I run both cases should pass Yep, let us see here actually Again this failed we can check expected one what got three calls? Three days expected one call. got three way maybe you didn't do something wrong. Oh, your times I should have given one. Oh, here this was not saved I guess it inline.
This was not saved sometimes there's a problem with stereo. I'll give the name as what Name and value is sub flow one. So sometimes is the problem in studio, even though we write it here, sometimes it might not have been saved. So make sure that you go here and verify in the XML whether it is written or not. Yep, now it is here. I think this was gone.
Number of times is one. Okay, now, if I run I hope this should run successfully right now. I'm running it again. Yep. The two test cases passed. Right?
Is it? Yeah, so you're able to verify that call. There are a lot of other features of a minute. I will see you next video.