Я пытаюсь выполнить несколько утверждений в тесте, JUnit останавливается на первом ошибочном утверждении.
Таким образом, чтобы иметь возможность сделать все утверждения и перечислить ошибочные в конце, я использовал класс ErrorCollector
и аннотацию @Rule
JUnit.
Вот пример тестового класса:
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();
в каждом тестовом классе.