如何使用Assert。抛出以断言异常的类型和实际的消息措辞?

就像这样:

Assert.Throws<Exception>(
    ()=>user.MakeUserActive()).WithMessage("Actual exception message")

我正在测试的方法抛出了多个具有不同消息的相同类型的消息,我需要一种方法来测试是否根据上下文抛出了正确的消息。

我已经用@Test注释编写了一些JUnit测试。如果我的测试方法抛出一个检查过的异常,并且如果我想断言该消息与异常一起,是否有一种方法可以使用JUnit @Test注释来做到这一点?AFAIK, JUnit 4.7不提供这个功能,但是将来的版本会提供吗?我知道在。net中你可以断言消息和异常类。在Java世界中寻找类似的特性。

这就是我想要的:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}

Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, 'x is less than zero' better or worse than if x < 0: raise Exception('x is less than zero') Also, is there any way to set a business rule like if x < 0 raise error that is always checked without the try/except/finally so, if at anytime throughout the code x is less than 0 an error is raised, like if you set assert x < 0 at the start of a function, anywhere within the function where x becomes less then 0 an exception is raised?