JUnit continue to assert things after expected exception

你。 提交于 2019-12-22 04:18:35

问题


I have something like

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

I believe the assertThat does not run as changeDirectory will throw the exception. Is it possible to make it still run?


回答1:


You could use a finally:

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



回答2:


I'd rather avoid any try/catch structures in unit-tests. Here's one possibility with Catch-Exception:

@Test
public void cdIntoNonExistantFolder() {
    catchException(cdTool).changeDirectory("nonexistant");

    assertThat(caughtException(), instanceOf(IllegalArgumentException.class));
    assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}

or with JUnit 5:

@Test
public void cdIntoNonExistantFolder() {
    expectThrows(IllegalArgumentException.class, () -> {
        cdTool.changeDirectory("nonexistant");
    });

    assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}



回答3:


I prefer avoiding using the expected exception annotation. The reason is often there are "arrange" statements prior to the "act" portion of a test. These "arrange" sections could thrown the expected exception and the test would pass with a false positive.

Instead, I would use a try/catch structure around the method-under-test.

@Test
public void cdIntoNonExistantFolder() {
    try {
        cdTool.changeDirectory("nonexistant");
        Assert.fail("Should have thrown IllegalArgumentException");
    }
    catch (IllegalArgumentException e) {
        Assert.assertTrue(true);
        assertThat(cdTool.getStatusCode(), not(equalTo(0)));
    }
}

Two things to note:

  1. The try block NEEDS to have an Assert.fail() method
  2. I use the Assert.assertTrue(true); statement in the catch block to show that this is the path intended.



回答4:


Create a new method, and put your assert in that method, annotate that method @After

@Test(expected = IllegalArgumentException.class)
public void cdIntoNonExistantFolder() {
    cdTool.changeDirectory("nonexistant");
}

@After
public void after() {
    assertThat(cdTool.getStatusCode(), not(equalTo(0)));
}


来源:https://stackoverflow.com/questions/21506079/junit-continue-to-assert-things-after-expected-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!