Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
1.1k views
by The go-to Tester (262 points)
Passing an invalid data throwing "NPE", which is expected. But how to assert those exception.Please provide me some example.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

There are two ways you can do this.

1.) By directly asserting the object is null.

e.g.

String str = null;
Assert.assertTrue(str == null, "String str is not null");
 
Or you can try using try catch block to catch NPE and assert it or log it.
 
try{
str.toLowerCase();
}catch(NullPointerException nullPointerException){
System.out.print("Null pointer exception");
//you can do your assertion here
}
 
Hope that helps!
by The go-to Tester (262 points)
a method returning NPF and i am assertiing it.

ex :
class A{
testmethod()
{
return null;
}
}

Test Class B {
test()
{
assert (testmethod);
}
by The go-to Tester (181 points)
Assert has to have two values right? Or there should be a boolean value. You can try something like below.

Assert.assertTrue(testmethod(),null);

Hope that helps!
by The go-to Tester (262 points)
@Test(expectedExceptions=NullPointerException.class)
{
.....
}

I am trying this , correct me if i am wrong
by The go-to Tester (181 points)
This looks good, but how do you see the future of this code? Tomorrow if you are asked to log or implement custom report is that possible to do so by using above code?


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...