@Before 和 @BeforeClass 有什么区别?
What is the difference between @Before and @BeforeClass?
题目类型: 技术面试题
这是一道技术面试题,常见于澳洲IT公司面试中。
难度: easy
分类: System Design
标签: @Before, @BeforeClass, JUnit4, JUnit5
参考答案摘要
答案 在题目中没有提供答案,因此这里给出一个通用回答:在 JUnit 4 中,@Before 在每个测试方法执行前运行;@BeforeClass 在所有测试方法执行前只运行一次,且必须是 static 方法。对应到 JUnit 5 分别是 @BeforeEach 与 @BeforeAll(@BeforeAll 默认也要求 static,除非使用特定测试实例生命周期配置)。
本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。