我尝试在测试中执行多个断言,JUnit 在第一个失败的断言处停止。
因此,为了能够执行所有断言并在最后列出失败的断言,我使用了该类ErrorCollector和 JUnit 的@Rule注释。
下面是一个测试类的例子:
public class MovieResponseTest {
/**
* Enable a test not to stop on an error by doing all assertions and listing the failed ones at the end.
* the <code>@Rule</code> annotation offers a generic way to add extended features on a test method
*/
@Rule
public ErrorCollector collector = new ErrorCollector();
/**
* A test case for <code>setCelebrity</code> Method
* @see MovieResponse#setCelebrities(List)
*/
@Test
public void testSetCelebrities() {
// Some code
this.collector.checkThat("The size of cast list should be 1.", this.movieResponse.getCast(), hasSize(1));
this.collector.checkThat("The size of directors list should be 1.", this.movieResponse.getDirectors(), hasSize(1));
this.collector.checkThat("The size of writers list should be 1.", this.movieResponse.getWriters(), hasSize(1));
}
}
现在我有另一个类,它的方法有多个断言。有什么办法可以@Rule通用,这样我就不必public ErrorCollector collector = new ErrorCollector();在每个测试类中都写了。
慕尼黑8549860
肥皂起泡泡
相关分类