Android JUnit Testing … How to Expect an Exception

后端 未结 3 1502
难免孤独
难免孤独 2021-02-04 23:21

I\'m attempting to write some tests using the built-in android Junit testing framework. I am running into a problem with a test where I am expecting an exception to be thrown.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 00:02

    The standard junit 3 idiom for this sort of test was:

    public void testThatMethodThrowsException()
    {
      try
      {
        doSomethingThatShouldThrow();
        Assert.fail("Should have thrown Arithmetic exception");
      }
      catch(ArithmeticException e)
      {
        //success
      }
    }
    

提交回复
热议问题