【问题标题】:try/catch a void type尝试/捕获一个 void 类型
【发布时间】:2014-11-10 11:36:26
【问题描述】:

我是编程新手,有一个菜鸟问题。我正在尝试进行这样的测试...

@Test
public void rememberTest()
throws DuplicateException{

    try{
        personA.remember(sighting4);
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
    try{
        assertEquals(personA.remember(sighting3), "The list already contains this sighting");
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
}

第一个 try/catch 编译,但第二个没有。它告诉我这里不允许使用“'void'”类型。 " 为什么我不能使用 void?如果我不能使用 void 类型,那么我将如何构建我的测试以便引发异常?

一些背景信息:rememberTest 是对将项添加到 ArrayList 的 remember 方法的测试。

Person类中的remember方法如下:

public void remember(final Sighting s)
throws DuplicateException
{
    if(lifeList.contains(s)) {
        throw new DuplicateException("The list already contains this sighting");
    }
    lifeList.remember(s); 
}

如果您需要更多信息,请提出要求,我会根据需要发布。

【问题讨论】:

  • remember方法的返回类型是什么?
  • personA.remember(sighting3) 必须返回无效。您不能将 void 类型作为其他方法的参数。
  • 嘿 Samngeeth,remember 的类型是 void
  • @nafas 那么我将如何构建此方法以便引发异常?这是我想要的信息。
  • @user3738926 更新您的问题并包含 remember 方法

标签: java junit try-catch void


【解决方案1】:

由于您的方法已经确保不会添加重复值,因此我建议从您的代码中删除 assertEquals

@Test
public void rememberTest()
throws DuplicateException{

    try{
        personA.remember(sighting4);
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
    try{
        personA.remember(sighting3), //this will throws Exception if sighting3 is already in.
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
}

演示如何编辑您的代码:

@Test
public void rememberTest()
throws DuplicateException{

    Sighting s1=//initialize s1
    Sighting s2=s1;
    try{
        personA.remember(s1);
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
    try{
        personA.remember(s2), //This will throw an exception because s1 and s2 are pointed to the same object
    }
    catch (Exception e) {
        fail("Failed" + e.getMessage());
    }
}

【讨论】:

  • nafas,这样看:假设Sighting3和Sighting4是一样的。我想添加Sighting3,然后添加Sighting4,并在测试中显示抛出了DuplicateException。
  • 这就是我的意思,mate,sighting3和sighting4可能是对同一个对象的引用,如果是这种情况,那么代码将抛出异常。从字面上看:sighting3 和Sighting4 都是对某些对象的引用。最重要的是对象本身而不是引用
  • 我确实解决了它,你帮助我解决了它。谢谢纳法斯。尝试{ personA.remember(sightingSame); } catch (DuplicateException e) { assertEquals("列表中已经包含这个目击事件", e.getMessage()); } catch (Exception e) { fail("失败" + e.getMessage()); }
【解决方案2】:

我认为你应该使用 @Expected 注释而不是断言,因为它是一个测试用例,所以它期望 DuplicateException

【讨论】:

    【解决方案3】:

    为了在测试类中抛出异常并捕获它,请执行以下操作:

     try{
                personA.remember(sightingSame);
            }
            catch (DuplicateException e) {
                assertEquals("The list already contains this sighting", e.getMessage());
            }
            catch (Exception e) {
                fail("Failed" + e.getMessage());
            }
    

    【讨论】:

      猜你喜欢
      • 2012-06-14
      • 2012-07-04
      • 2019-08-25
      • 1970-01-01
      • 2016-10-08
      • 2014-06-09
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多