JUnit @Rule和ExpectedException.none()线程安全吗?

时间:2018-09-17 23:38:19

标签: java junit thread-safety

我正在编写一个JUnit测试,并且希望它测试是否引发了异常,因此在下面的Java测试类中编写了这本书(这不是我编写的实际JUnit测试,而是说明了我试图克服的观点的一个测试) )。

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testMethod1() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Parameter cannot be null");

    UtilClass.getSampleValue(null, new Object());
}

@Test
public void testMethod2() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Parameter cannot be null");

    UtilClass.getSampleValue(new Object(), null);
}

@Test
public void testMethod3() {
    final Object actual = UtilClass.getSampleValue(new Object(), new Object());
    assertNotNull(actual);
}

当我发送此代码进行代码审查时,有人在评论中写道,使用@Rule意味着测试方法不是线程安全的,将来如果我们想运行{{3} }这是行不通的,因为没有预料到异常的测试实际上会看到异常。这是真的?有没有解决方法?我在网上找不到与此有关的任何文档。

0 个答案:

没有答案