我有类似的东西

@Test(expected = IllegalArgumentException.class)
public void cdIntoNonExistantFolder() {
    cdTool.changeDirectory("nonexistant");
    assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}

我相信assertThat不会运行,因为changeDirectory会抛出异常。是否有可能使其继续运行?

最佳答案

您可以使用finally:

@Test(expected = IllegalArgumentException.class)
public void cdIntoNonExistantFolder() {
    try {
        cdTool.changeDirectory("nonexistant");
    }
    finally {
        assertThat(cdTool.getStatusCode(), not(equalTo(0)));
    }
}

09-07 12:08