IInvokedMethodListener

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.52
List Price:  €95.03
You save:  €28.51
£55.29
List Price:  £78.99
You save:  £23.70
CA$98.01
List Price:  CA$140.02
You save:  CA$42.01
A$108.26
List Price:  A$154.67
You save:  A$46.40
S$94.28
List Price:  S$134.69
You save:  S$40.41
HK$544.68
List Price:  HK$778.14
You save:  HK$233.46
CHF 62.24
List Price:  CHF 88.92
You save:  CHF 26.68
NOK kr781.73
List Price:  NOK kr1,116.81
You save:  NOK kr335.07
DKK kr496.25
List Price:  DKK kr708.95
You save:  DKK kr212.70
NZ$119.48
List Price:  NZ$170.70
You save:  NZ$51.21
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,430.24
List Price:  ৳12,043.71
You save:  ৳3,613.47
₹5,907.88
List Price:  ₹8,440.19
You save:  ₹2,532.31
RM313.59
List Price:  RM448
You save:  RM134.41
₦117,896.75
List Price:  ₦168,431.15
You save:  ₦50,534.40
₨19,601.13
List Price:  ₨28,002.81
You save:  ₨8,401.68
฿2,456.64
List Price:  ฿3,509.64
You save:  ฿1,053
₺2,405.01
List Price:  ₺3,435.88
You save:  ₺1,030.86
B$408.16
List Price:  B$583.12
You save:  B$174.95
R1,278.38
List Price:  R1,826.34
You save:  R547.95
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩98,547.64
List Price:  ₩140,788.38
You save:  ₩42,240.74
₪262.83
List Price:  ₪375.48
You save:  ₪112.65
₱4,119.38
List Price:  ₱5,885.08
You save:  ₱1,765.70
¥10,926.10
List Price:  ¥15,609.38
You save:  ¥4,683.28
MX$1,438.75
List Price:  MX$2,055.45
You save:  MX$616.69
QR257.25
List Price:  QR367.52
You save:  QR110.26
P957.42
List Price:  P1,367.80
You save:  P410.38
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,455.33
List Price:  E£4,936.40
You save:  E£1,481.07
ብር8,611.91
List Price:  ብር12,303.26
You save:  ብር3,691.35
Kz63,807.12
List Price:  Kz91,156.94
You save:  Kz27,349.81
CLP$68,583.90
List Price:  CLP$97,981.20
You save:  CLP$29,397.30
CN¥506.99
List Price:  CN¥724.30
You save:  CN¥217.31
RD$4,249.83
List Price:  RD$6,071.45
You save:  RD$1,821.61
DA9,380.49
List Price:  DA13,401.28
You save:  DA4,020.78
FJ$159.39
List Price:  FJ$227.72
You save:  FJ$68.32
Q545.12
List Price:  Q778.78
You save:  Q233.65
GY$14,759.55
List Price:  GY$21,085.97
You save:  GY$6,326.42
ISK kr9,759.40
List Price:  ISK kr13,942.60
You save:  ISK kr4,183.20
DH701.60
List Price:  DH1,002.33
You save:  DH300.73
L1,265.07
List Price:  L1,807.32
You save:  L542.25
ден4,092.25
List Price:  ден5,846.32
You save:  ден1,754.07
MOP$565.22
List Price:  MOP$807.49
You save:  MOP$242.27
N$1,267.59
List Price:  N$1,810.92
You save:  N$543.33
C$2,596.85
List Price:  C$3,709.94
You save:  C$1,113.09
रु9,521.24
List Price:  रु13,602.36
You save:  रु4,081.11
S/267.43
List Price:  S/382.06
You save:  S/114.63
K283.56
List Price:  K405.11
You save:  K121.54
SAR262.97
List Price:  SAR375.69
You save:  SAR112.72
ZK1,929.77
List Price:  ZK2,756.93
You save:  ZK827.16
L331.05
List Price:  L472.96
You save:  L141.90
Kč1,682
List Price:  Kč2,402.96
You save:  Kč720.96
Ft27,138
List Price:  Ft38,770.23
You save:  Ft11,632.23
SEK kr772.63
List Price:  SEK kr1,103.81
You save:  SEK kr331.17
ARS$69,869.10
List Price:  ARS$99,817.28
You save:  ARS$29,948.18
Bs487.60
List Price:  Bs696.60
You save:  Bs209
COP$315,227.33
List Price:  COP$450,344.06
You save:  COP$135,116.73
₡36,031.09
List Price:  ₡51,475.19
You save:  ₡15,444.10
L1,781.45
List Price:  L2,545.04
You save:  L763.59
₲550,680.18
List Price:  ₲786,719.69
You save:  ₲236,039.51
$U2,973.65
List Price:  $U4,248.26
You save:  $U1,274.60
zł288.46
List Price:  zł412.11
You save:  zł123.64
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this one, we will actually start our discussion on test engine listeners. So listeners are classes which waits for a particular event to occur. And when that event happens are the respective listener is going to trigger any logic that we want. Let's imagine a real life simple scenario, I want my microwave to make a sound when it completes heating of anything. There should be some program for it right?

There should be some program that is continuously listening to microwaves, events. And when microwave timer is off, and it has completed heating water or any other food item, it should make a sound so that I know it's done. This is a very simple example of a listener in real life. Similarly, test energy also provides us different kinds of listeners. And the first one We're going to take a look at is I invoked method listener. So let's start with this.

I have created couple of things already. And we're gonna work on the main thing right now. So this is test mg underscore listeners, test one class, I have a before class inside it, it just says it prints Gordon before class, and then we have an after class. And then we have a test method one, which is gonna be successful true. And then we have a test method two and it's gonna be failed because we are asserting false we are to the assert true method, right? And this class is under test classes packet.

So basically this one, and we can take a look at it here, right? And then I created one XML file and named it test and G dash listeners dot XML and its name is listeners test suite, application test and the class name it references test classes, dot test nj listeners, test one. Now let's go ahead. And also, I created a package and named it as listeners packet. Now let's go ahead and create our first custom listener. So basically, what we want to do here is we want to create a class that listens to all our test cases.

And when something happens, maybe when my test case starts, or my test case stops, or maybe if the test cases successful, or if it fails, or if it skipped. Basically, there are so many events that can occur right? In our testing scenarios. So basically, we want to create a listener custom listener class, which listens to all these events continuously. And its internal test and it takes care of it for us. And then we can provide what we want to do when something happens.

So right click this One, create a new class and call it custom lists no one, because we're going to create multiple listeners in different lectures. So custom listener one, and finish. So now we have an empty class up there, right? Let's see what we can do here. So what I'm gonna do here is, I am going to, because all these listeners, they are interfaces, so we need to implement the methods inside those interfaces in Java. So we're gonna say implement, and I in Vogue, so I typed i n v, and then I pressed Ctrl Space and it gives me all these options.

So I'm gonna pick the one that I'm have selected right now I invoked method listener. And this is from our dot test. NT. and hit enter. Once I do that the error shows up on the class name. And it says that we must implement the inherited abstracted method from this class from this interface.

So I'm going to click on Add unimplemented methods, and it's all happy, everything is fine. And in imported few packages, make sure you import the correct ones. If something happens, make sure you can fix them manually also, like if an imports are wrong one you can actually go ahead and type it manually. So let's minimize this. And what we have here is in this interface, we have two methods before invocation and after invocation. And it takes in two arguments I invoke method, method is the name and this is the type and I test result which we have already learned.

And test result is the variable name. Now this is the place where we can add our blood We can add any code which will take place when this action happens and let's understand what understand what this is. So, before invocation and after invocation is nothing, whenever any method is run inside our test engine test class before running that method, this method will be run. So, basically this method will be run before any method runs in the test class. So, let's say my test and G underscore listeners test one class uses the custom listener one. So anything happens so if before this class sorry before test method, the setup method, the before invocation method will be run.

And then before the cleanup method also this will be run and before test methods also, this will be run so before invocation means it will run before any method In the test class, so let's write it in the comment. So we know what's happening before every method in the test class. And the same thing applies to after invocation. So the only differences, it's gonna be executed after every method in the test class. So now let's see what we can do. We can implement any logic here, but right now we're gonna actually fetch the name of the class where the test method is executed, and the name of the method, the actual test method that is executed.

So I'm going to print here, after sorry, before invocation, so we know that it's coming from this method, the before invocation method and we can actually print like our or maybe we can actually Use this thing. method is the name. So see, you can actually see a few differences. Sometimes you might see here are zero and r1. So don't worry about the variable names because this is just a variable name, you can you can actually change it anything, right? You can change it anything.

The important part is the type and whatever name you see here, you can use it. So I'm going to use this method and.or maybe you know what let's first print the class name. So I'm going to use the test result variable. So test result dot get class or maybe get test class. So get test class is going to give us the class name where the methods exist, and then dot get name. So this thing will give us the name Have the test class where the code was written.

And then plus, and then let's hit enter. So we have some space. And just a little bit decent formatting, so it's easy for us to understand. Plus, now we're going to use the method variable. This is from I invoke method dot get test method dot get method name. So, what we have done here is we have seen that we are using the test variable test result variable also to get the class name.

And we are making use of the method variable which is from invoke method to get the name of the method, we can actually use the test result from a test result also to get the name of the method, but I wanted to show you both things, right. So, we are just doing this thing here. Now, let's just copy this here to our after invocation method. Just change the after invocation thing. And that's about it. Now, I actually wanted to run this test and G listeners XML file and show you something so run as test in G Suite.

So what it ran it ran colon before class, then code in test method one code and test method two and code and after class, whatever we have here in the test and G classes, right, that's all we have. Now, how do we know how to basically this class knows about the custom listener? We we implemented something here, but we haven't told this listeners test one class that there is some custom listener and you can make use of it. We haven't done anything like that. So basically, there is a add the rate listeners annotation In test Angie, and there are a couple of ways we'll take a look at both of them. And by step by step.

So listeners, this one, I'm gonna select the first one or dot test engine dot annotations. And inside the parenthesis, I need to provide the name of the listener. So custom listener, I need to type something before I can hit control space, so it knows and it tells us that oops, the wrong one. Custom listeners, one, this one, we want custom listener one and it's from the listener package. So and we saw that we implemented this and listeners package. So we want to make sure that we're getting the right thing and dot class.

And then we can see that Oops, wrong package was also important that it imported the correct package. Also listeners package custom listener one. And it's really needed because our test class is inside the test classes package. And the custom listener one is inside the listeners package. So we want to make sure that we import the correct stuff here. So that's about it.

That's done. Now, I can just right click my XML file, and click run as test in G Suite. Now we see the magic what's happening here. Before invocation method was done, it is running for test classes dial test, Angie underscore listener, test one class and then it's printing the method name setup, right? Then colon before classroom so before class is the setup. We can see that right Gordon before class is the setup method.

And then after invocation was done right, and then again before invocation was done and then test method one was executed, then code executed from test method one then after invocation executed for test method one. Then again before invocation for test method two then code inside test method to execute it then after invocation for test method, and then the same steps happen before invocation for cleanup coding after class and then after invocation for cleanup. So, basically, we can see that we implemented a custom listener here we can write any logic here and that will be executed when these methods were running. And before invocation and after invocation executed before and after every single method. It doesn't have to be the test method. This is a very important point to understand that it does not have to be the test method.

It is running before the session method also where we had the before class, it is running after on before and after the cleanup method also where we have the annotation as after class and it is running before and after the annotations for added a test also. And if we had the annotations for at the rate before method or at the rate after method, it will run for them as well. So, basically the important part to understand here is I invoke method listener interface, both the methods inside this interface, they are run for every single method in our test class. So if we want to make use of anything like that, if you want to run anything before and after every single method in our test class, if we want to put any logic inside there, we can create a custom listener we can implement this interface and create these methods.

We are overriding it obviously this part was done for us when we clicked on add unimplemented methods and that's about it. We're done. So that's about it guys. Please download the code files practice and let me know if you have any questions. Thanks

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.