当@before 部分的代码失败时,Junit 会执行@Test 用例吗?
Will Junit execute the @Test case when the code in the @before Section fails?
我是 Junit 新手。
如果我在 @before 部分执行某些操作,我的代码就会失败。例如我想建立一个服务器连接,但由于某种原因连接失败。
即使@before 失败,Junit 是否会在@Test 部分执行我的测试?因为那时我的测试会失败,因为我没有服务器连接。如果是这种情况我该如何处理?
你可以用一个基本的 junit 来试试这个。
public class SamplePlayTest {
@Before
public void setup() {
System.out.println("Before called");
throw new RuntimeException();
}
@Test
public void test() {
System.out.println("Test case called");
}
}
输出为
Before called
并因错误 java.lang.RuntimeException
而失败。 Test case called
未打印。
我是 Junit 新手。 如果我在 @before 部分执行某些操作,我的代码就会失败。例如我想建立一个服务器连接,但由于某种原因连接失败。
即使@before 失败,Junit 是否会在@Test 部分执行我的测试?因为那时我的测试会失败,因为我没有服务器连接。如果是这种情况我该如何处理?
你可以用一个基本的 junit 来试试这个。
public class SamplePlayTest {
@Before
public void setup() {
System.out.println("Before called");
throw new RuntimeException();
}
@Test
public void test() {
System.out.println("Test case called");
}
}
输出为
Before called
并因错误 java.lang.RuntimeException
而失败。 Test case called
未打印。