Method And Class Annotations

8 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. So in this one, we're gonna take a look at couple of more annotations. So let me just bring the eclipses there. So right click on the package, go to new. And we will create a new test NZ class. And I'll show you these things in this lectures.

So there are these annotations right before method after method before class after class, before suite after suite. There's one called before test after test also, we don't use these too much. So that's why we're going to focus on these six. In this lecture, we'll take a look at before method after method before class after class. And we will take a look at before sweet after straight later. And there's one called data provider which we will have a separate lecture for.

So right now I'm gonna do this thing. I'm gonna select this one, and this one from here and I'll show you that test. Angie crazy. The template for us if we select this, and I'm not selecting these because I want to type them and show you that it's not necessary to select from here, we can actually go ahead and type them all. So click Finish. And it created the class as new test because we did not change the name.

And what we can do here is we can select this right click and find refactor somewhere here, rename. And I'm gonna say as test Angie. And maybe I'm gonna say before class annotations. So just a meaningful name. And it's going to change the file name also here, it takes care of it. And now you can see that it created a test method with test annotation.

Then it created a before method and after method It added annotations here, and then you can see it imported the package is also on its own. Now we can select the code and do Command Shift F or Ctrl, Shift F, and it looks better aligned. Then what I want to tell you here is this before method name, this is not important. You can name it anything. The important part is this annotation. So the name could be anything, whatever whatever method has, this annotation will be in effect.

And what this means is, let's say we have multiple test methods. So let me just change the name test method one. And let me copy this one more time test method two. And now we have two test methods. Let me put it on the top before method. After method, so it's just easy to see what's happening before and after.

And at the bottom, we can take a look at what other tests doing. So let's just print out that the test is running. And maybe I can just copy it from one of the previous test that the sprint line. So let's put it here. Running test, and this is test method one. And this is running test.

This is test method two. So let's assume a scenario where we have some common steps that we wanted to execute before every test method starts. And then there are some common steps that we wanted to execute after every method ends. So we don't want to put those repetitive steps here in Every test method right. Instead of that what we can do here is we can put those steps in before method and after method. So, if we put any step here, that will be executed before any test method is executed.

So, I am going to put this thing here and modify this maybe. So, we will say this runs before every method and we'll copy the same thing from here. And this runs after every method, right, same thing exactly same so before method, it will run once before every method and after method, this method this will run Once after every method, so let me save this file and run it, and I'll show you the output. So, you can see here, this once before every method, then it runs the test method one, then the after method was executed. This runs after every method. Then again before method is executed this once before every method because it has to run test method two, then it ran test method two, then it again ran after method and prints the line this runs after every method.

Well, the tests are successful because we are just printing out some values. So that is the use of before method and after method. It's very handy very useful. If we want to run something common before and after every method, every single method we can do that. Now we will take a look at something called before class and after class. I'm just going to type public void And maybe we can name it as setup.

I'm begging just name it anything. And then here the sky. And let me just copy this thing, and I'll paste it again. And instead of setup, I can type in hair cleanup. So why I'm doing this is because in setup, I will say add the red before and hit control space, it will give me all the options. I'll select before class from or dot test ng g dot annotations please do not confuse these with J unit.

I'm saying it again and again because I've seen a lot of errors around this time. And this one, we can just simply type something. So this runs once before all methods so it not all basically this is gonna be confusing before I can just simply say before class and we can just copy this from here, paste this guy here. And here we need to use the annotation called after control space and after class. So this is how we can create these methods by typing also we don't have to necessarily select from there. So, we can say here this runs after class.

Now let's execute this class and we will see the output and just a second. So, what does it show now it says this runs once before class, and then the before method executed, then the test method executed, then after method executed, then again the before method executed, then the test method, then after method and then finally, the cleanup after class. So, you'll notice that the difference is the before method is executed once before every method after method is executed once after every method, but before class is executed once at the beginning, then it will run all the test methods and then after class, this is executed once after all the methods are executed. So, this is before class and after class and before method and after method, they are really very handy, very useful. So, please go ahead, practice around it just create your classes. You can also use this code and I'll see in the next lecture.

Thanks for attending this one. Thanks a lot.

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.