Hello, all welcome back to the next one. In this one, we will take a look at I test result interface. So let me just bring up Eclipse. And I've created a class called test mg I test result demo. And I think it's here somewhere. So in the class, we have test method one, test method two and one after method.
And I'll explain you what all means. One thing I want to mention is please do not use the exact results from test and D like do not just use a test result. Always append something or add something on before because if we use the same names from an existing library, we might have issues when we run the code. So now let's take a look. Other thing is I'm using assert here so please make sure that we use a third from test Angie because lots of people have j unit libraries also And then you will have issues. So what we have right now here is we have a print statement inside test method one that it's running test method one, and then we are asserting false.
So basically, we are asserting false to assert true. So which means test method one is going to fail. And in test method to again a print statement, and we're asserting true to assert true. So which means test method two is going to be successful. Now what we want to do here is we want something or some way by which we know what was the result of the previous method, which was run. So let's say if test method burn was run, I want to know what was the result like if it was successful, or it failed.
So for those things, test engine has provided an interface called I test result. And this can be used to figure those things out, figure out the status of the last run test method and this interface is like a listener, it's kind of listening every time. And after every test method is run, it knows the status of the last run method. So we can use it to fetch the status. Now, we can use it in combination with after method, because after method is always run after every test method. So basically, if we use the itis listener here, we will be able to fetch out the result, right.
So to use the eye test result, what we want to do here is we will provide an argument to the after method here, so I test result, and I'm just gonna name the argument as test result. So I test result is the type and test result is our name of the argument. I'm going to hover the mouse on it, and I'm gonna click on Import itis result from our dot test, mg, all Make sure we import the correct thing. Once we have it, we can actually kind of bring the status. So let me just say, See, so, and I'm gonna print maybe the method name first right now. So we'll see we can use it in different ways.
So test result, dot, get method, and dot get method name. So if we see this, basically, test result, and this guy returns, it says it returns this method. This the test method, this result represents. So basically, the get method is the test method in which the result is representing. And then get method name. It returns the method name.
And it's usually Yeah, it's obviously the gonna be the method name. In which we ran the test cases. So that's what we are running right now. And we can also get the status and do those things. So let me first run it right now. And we will just go step by step.
So run as test in G test. And we can see a pair that test method one is failed, because we expected true, but we know that we provided false so it's gonna fail. And test method two is successful. In console we can see running test method one is coming from test method. And this is coming from after method, test method, one, the name of the method and then running test method two, and test method to the name of the method. So basically, that's what we printed here, right.
So we know that in after method, we could fetch the name of the test method, which was run just before it so first time it returned us test method one and second time test method two Which is pretty cool. And we can use it in lots of different ways. So now let me just do one more thing. So I'm going to do F. So based on the status of the last test case, we can print few things, or we can do some actions. Or if we want to do something based on the previous result, let's say my test was failed, and I want to take a screenshot, or my test was successful, and I want to report something I want to enter something into the database. I mean, anything you can do, depending on your application or your needs, but the important part is we have the capabilities to take actions based on the result of the last test case.
So I'm going to say, test result, dot get status. So this thing is gonna return us so you can see on the left, the status of this result using one of the constants above so basically, get status equals two Then constant we want to use here is I test result. And that should be exactly same right is result dot failure. So let's say I'm based on the failure, I'm gonna print something. So let me just copy this thing here. And I can add a few more things to make it a little easier to understand.
So because it's on the test case, fail, I'm printing here failed, plus the name of the method that failed. So basically, that's what I'm going to say. And I'm going to use the same thing one more if condition. And in this one, we can say I test result dot started success. So basically, we wonder one from success. And here I'm gonna say successful and the name of the method.
I want to show you what way to get the name of the method. So basically, instead of doing get method dot method name, we can just say get name. And if I hover my mouse, you can read the description. So let me actually bring it on little bit up. So you can read the description it says the name of this test result, and typically it is identical to the name of the method. So we can just do this as well.
Now we have both the test methods, and we have the after method, and we are printing the results based on it right? So let me right click it run as test in G test. And let's see the output. So running test method one, and failed test method one, running test method two and successful test method two. So these two lines are coming from our after method, which is using the eye test method. Right.
And the further ones this is from the test engine report, which by default happens and this exception or error is from the failed test case. And we already know about this one. So it's all expected. Let's take a look at the import statements. import statement for this is our dot test nj dot test result. So that's about it.
It can be used in many ways, as we discussed. So please go ahead, download the code file, practice it. Let me know if you have any questions and I'll see you in the next lectures. Thanks for attending this one. Thanks.