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
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
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.