【发布时间】:2018-01-15 20:10:00
【问题描述】:
从我的教授那里,我得到了一个类似这样的测试文件:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class SomeObjectTest {
@Test
public void testIstPalindrom() {
SomeObject obj = new SomeObject();
assertThat("MSG 1", obj.someFunction(" "), is(equalTo(true)));
assertThat("MSG 2", obj.someFunction("A"), is(equalTo(true)));
assertThat("MSG 2", obj.someFunction("B"), is(equalTo(false)));
}
}
由于我从未使用过hamcrest,我现在有点困惑。通常我只会对每个Test 使用一个assert 调用。我使用以下命令编译并运行此测试:
javac -cp ".;hamcrest-all-1.3.jar;junit-jupiter-api-5.1.0.jar" SomeObjectTest.java
java -cp ".;hamcrest-all-1.3.jar;junit-jupiter-api-5.1.0.jar;junit-4.12.jar;" org.junit.runner.JUnitCore SomeObjectTest
当我运行这个测试时,如果只有一个 assert 调用失败,它也会失败。这是因为他们都在一个测试中,还是我应该在这里使用另一个测试运行器?
【问题讨论】:
标签: java unit-testing junit hamcrest