1

I am doing Android Unit Test Case Execution and for Negative Test Case I should get exception, but for some API's Exception is not caught.

Please do find the example below:

public void testInsertSenderType_n() { DBSms obj = new DBSms(getContext()); obj.open(); int i =0; int a =0; boolean result = true; i=obj.GetToatlCount(); obj.insertSmsText(i+1,"Hello to testInsertSenderType_n"); a=obj.TotalcountSms("Inbox"); try { obj.insertSenderType(-100, "Richard", "Inbox", 0); } catch (Exception e) { // TODO: handle exception result = false; } assertEquals(a,obj.TotalcountSms("Inbox")); assertEquals(false,result); obj.close(); } 

Here in, obj.insertSenderType(-100, "Richard", "Inbox", 0); should throw an exception. But it is not thrown.

Please do guide where can I be Wrong.

1
  • So, the method should throw an exception, and the unit test shows that it's not the case, is that right? If so, congratulations: you've just discovered a bug in the code of DBSms thanks to your unit tests. Fix it and check that the unit test passes once the fix is applied. Commented Dec 22, 2011 at 7:46

1 Answer 1

1

I use following method to expect proper exception:

try { doSomethingToProvokeException(); fail("there ought to be an exception dude, but was not"); } catch(ExeptionIHaveProvoked ex) { doAssertionnsonThrowsException } 

You do not need variables to keeps exception state. As for why no exception is thrown in your code - nobody cann tell it to you, unless you provide source of object.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.